I am creating simple tkinter app using python. My app was working fine but code was mess. Now I am trying to organize my code using class and methods, but facing issue in same FYI - am new to python Below is my code. Logically I have to call tkobject.kky_mnframe_func() before tkobject.frames() as tkobject.kky_mnframe_func() is used as command for one of my tkobject.frames() buttons. But if I do so it is giving error as variable self.ky_entr used in tkobject.kky_mnframe_func() is defined in tkobject.frames() and it need to be called first. So inshort am not able to proceed either way. Any python pro here, who can guide me handle this better way.
class TkinterSuperbot:
def get_git_key(self):
self.headers = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}
self.login_data = {
'commit': 'Sign in',
'utf8': '%E2%9C%93',
'login': '*******',
'password': '******'
}
with requests.Session() as s:
self.url = "https://github.com/session"
self.r = s.get(self.url, headers=self.headers)
self.soup = BeautifulSoup(self.r.content, 'html.parser')
self.login_data['authenticity_token'] = self.soup.find('input', attrs={'name': 'authenticity_token'})['value']
r = s.post(self.url, data=self.login_data, headers=self.headers)
self.json = s.get('https://github.com/*****/****/***/***/***.json')
self.json.status_code
self.json_soup = BeautifulSoup(self.json.text, 'html.parser')
self.key = self.json_soup.find('span', {'class':'pl-c1'}).text
return self.key
def kky_mnframe_func(self,event = None): # evevn none is for entery keyboard
if self.ky_entr.get()==self.get_git_key():
self.mn_frm.pack(fill='both', expand=1)
self.ky_frm.forget()
else:
messagebox.showerror('Access Denied!', 'Invalid Key')
def frames(self):
self.window=Tk()
self.window.geometry('522x300')
# window.eval('tk::PlaceWindow . center')
# Gets the requested values of the height and widht.
self.windowWidth = self.window.winfo_reqwidth()
self.windowHeight = self.window.winfo_reqheight()
print("Width",self.windowWidth,"Height",self.windowHeight)
# Gets both half the screen width/height and window width/height
self.positionRight = int(self.window.winfo_screenwidth()/2.45 - self.windowWidth/2)
self.positionDown = int(self.window.winfo_screenheight()/3 - self.windowHeight/2)
# Positions the window in the center of the page.
self.window.geometry("+{}+{}".format(self.positionRight, self.positionDown))
self.window.iconbitmap('zicon.ico')
self.window.title('SuperBot')
self.window.configure(background='light gray')
self.window.resizable(width=False, height=False)
self.ky_frm = LabelFrame(self.window, text='Enter Authorisation Key', labelanchor='n', pady=0, padx=10, borderwidth=2,background='light gray', font=('arial', 9, 'bold'))
self.ky_frm.pack(fill='both', expand=1, padx=80, pady=100)
self.window.bind('<Return>', self.kky_mnframe_func) #for enter key board
self.ky_entr = Entry(self.ky_frm, show='*', width=21, borderwidth=2, justify=CENTER)
self.ky_entr.place(x=101, y=10)
self.ky_bttn = Button(self.ky_frm, text='Submit', background='light gray', font=('arial', 10, 'bold'), width=15, command=self.kky_mnframe_func, cursor='hand2')
self.ky_bttn.place(x=101, y=40)
self.ky_bttn.bind('<Return>')
self.self.mn_frm = Frame(self.window, background='light gray')
self.label1 = Label(self.self.mn_frm, text='"SuperBot" Welcomes You', font=('arial',18,'bold'), relief='flat', fg='blue2', bg='light gray')
self.label1.place(x=107, y=240)
self.button_login= Button(self.mn_frm, text='Login', width=8, font=('arial',10,'bold'), relief='raised', fg='white', bg='cornflower blue', command=scrp.linked_login, cursor='hand2')
self.button_login.place(x=2, y=0)
self.button_upload = Button(self.mn_frm, text='Upload',width=8, font=('arial',10,'bold'), relief='raised', fg='white', bg='cornflower blue', command=scrp.read_links, cursor='hand2')
self.button_upload.place(x=76, y=0)
self.button_getprof = Button(self.mn_frm, text='Get',width=8, font=('arial',10,'bold'), relief='raised', fg='white', bg='cornflower blue', command=scrp.multi_link_scrpr, cursor='hand2')
self.button_getprof.place(x=150, y=0)
self.button_clear = Button(self.mn_frm, text='Clear',width=8, font=('arial',10,'bold'), relief='raised', fg='white', bg='cornflower blue', command=scrp.clear_list, cursor='hand2')
self.button_clear.place(x=224, y=0)
self.button_download = Button(self.mn_frm, text='Download',width=8, font=('arial',10,'bold'), relief='raised', fg='white', bg='cornflower blue', command=scrp.download_profile, cursor='hand2')
self.button_download.place(x=298, y=0)
self.button_link = Button(self.mn_frm, text='Links',width=8, font=('arial',10,'bold'), relief='raised', fg='white', bg='cornflower blue', command=scrp.group_scroll, cursor='hand2')
self.button_link.place(x=372, y=0)
self.button_connect = Button(self.mn_frm, text='Connect',width=8, font=('arial',10,'bold'), relief='raised', fg='white', bg='cornflower blue', command=scrp.group_scroll, cursor='hand2')
self.button_connect.place(x=446, y=0)
self.my_image = ImageTk.PhotoImage(Image.open('zlogo.png'))
self.img_label = Label(self.mn_frm,image=self.my_image)
self.img_label.place(x=157, y=40)
self.img_label.configure(background='light gray')
self.window.mainloop()
tkobject = TkinterSuperbot()
tkobject.get_git_key()
tkobject.kky_mnframe_func()
tkobject.frames()
tkobject.kky_mnframe_func()need to be executed beforetkobject.frames()?