I am trying to use a entry widget for password input, a handler function for password verification and a label widget for displaying the result of the verification. The code is as following so far,
class LoginFrame(Frame):
def __init__(self, parent):
#Frame.__init__(self, parent)
super(LoginFrame, self).__init__()
self.parent = parent
self.initUI()
# initialize the login screen UI
def initUI(self):
# Set up login frame properties
self.parent.title("Login Screen")
# creating instruction label
inst_lbl = self.make_label(self.parent, "Please login to continue")
# creating labels and entries for user name and password
user_name = self.make_entry(self.parent, caption="User Name:")
pwd = self.make_entry(self.parent, caption="User Password:", show="*")
# create a login button
login_btn = self.make_button(self.parent, self.verify_user, "Login")
# create a button
#----------------------------------------------------------------------
def make_button(self, parent, command, caption=NONE, side=TOP, width=0, **options):
"""make a button"""
btn = Button(parent, text=caption, command=command)
if side is not TOP:
btn.pack(side=side)
else:
btn.pack()
return btn
def make_label(self, parent, caption=NONE, side=TOP, **options):
label = Label(parent, text=caption, **options)
if side is not TOP:
label.pack(side=side)
else:
label.pack()
return label
def make_entry(self, parent, caption=NONE, side=TOP, width=0, **options):
#Label(parent, text=caption).pack(side=LEFT)
self.make_label(self.parent, caption, side)
entry = Entry(parent, **options)
if width:
entry.config(width=width)
if side is not TOP:
entry.pack(side=side)
else:
entry.pack()
return entry
# verify user name and password
#----------------------------------------------------------------------
def verify_user(event):
"""verify users"""
if user_name.get() == "admin" and pwd.get() == "123":
#inst_lbl.configure(text="User verified")
event.widget.config(text="User verified")
else:
#inst_lbl.configure(text="Access denied. Invalid username or password")
event.widget.config(text="Access denied. Invalid username or password")
def main():
top = Tk()
app = LoginFrame(top)
top.mainloop()
if __name__ == '__main__':
main()
now, I need user_name and pwd to be verified by the method verify_user, the result is shown in inst_lbl whenever the button login_btn is clicked (to trigger the verification). So how to bind inst_lbl to verify_user, in response to the login_btn click event? and how to get the contents of user_name and pwd to get verified in verify_user?
Make them class variable by adding self. prefixes. This way you can access them outside of the method they are created.
self.inst_lbl = self.make_label(...)
self.user_name = self.make_entry(...)
self.pwd = self.make_entry(...)
When trying to call them, you also will need self. prefixes.
self.inst_lbl.configure(...)
Other than that, you'll be needing self parameter in verify_user method since it's a member of a class.
Also, for Python-2.x, you'll get TypeError: must be type, not classobj on your super(LoginFrame, self).__init__(). The line you commented out is the right one to use.
Frame.__init__(self, parent)
If you want to use super(), you should add object as parameter to your class definiton because of old style classes etc.
class LoginFrame(Frame, object):
Related
I am new to Python, and especially GUI Python, and am trying to figure out how to add two functions to my button.
For example, I want user to be able to:
click on button normally and button to execute function one
shift-click on button and button to execute function number two.
I am using tkinter for GUI.
Code for button:
Any help is appreciated.
b1 = Button(window, text = "Import", width = 12, command = functionOne)
b1.grid(row = 0, column = 2)
You can do something like this - Instead of setting the button's command keyword argument, just bind different events captured by the button to different functions. Here's some more info about events and binding events.
import tkinter as tk
class Application(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.title("Test")
self.geometry("128x32")
self.resizable(width=False, height=False)
self.button = tk.Button(self, text="Try Me")
self.button.pack()
self.button.bind("<Button-1>", self.normal_click)
self.button.bind("<Shift-Button-1>", self.shift_click)
def normal_click(self, event):
print("Normal Click")
def shift_click(self, event):
print("Shift Click")
def main():
application = Application()
application.mainloop()
return 0
if __name__ == "__main__":
import sys
sys.exit(main())
I defined a method for creating a label with the following code,
from tkinter import *
class LoginFrame(Frame):
def __init__(self, parent):
super(LoginFrame, self).__init__()
self.parent = parent
self.initUI()
# initialize the login screen UI
def initUI(self):
self.parent.title("Login Screen")
# Set up login frame properties
self.parent.title("Login Screen")
# create a background image
photo_bg = PhotoImage(file="building.gif")
building = self.make_label(self.parent, image=photo_bg)
# creating instruction label, where the error occurs
inst_lbl = self.make_label(self.parent, "Please login to continue")
def make_label(parent, caption=NONE, side=TOP, **options):
label = Label(parent, text=caption, **options)
if side is not TOP:
label.pack(side=side)
else:
label.pack()
return label
def main():
top = Tk()
app = LoginFrame(top)
top.mainloop()
if __name__ == '__main__':
main()
I got an error when tried to run the code,
_tkinter.TclError: bad side "Please login to continue": must be top, bottom, left, or right
I am wondering how to resolve the issue? There was no error when creating the back ground image using make_label.
Your definition of make_label is missing the self parameter. You need to define it as this:
def make_label(self, parent, ...):
I put a event handler in a class Verification in a separated file verification.py (tried to make the code modular). The main GUI code is as follows,
from tkinter import *
from make_widget import *
from verification import *
class LoginFrame(Frame):
def __init__(self, parent):
super(LoginFrame, self).__init__()
self.parent = parent
self.initUI()
# initialize the login screen UI
def initUI(self):
# Set up login frame properties
self.parent.title("Login Screen")
# creating instruction label
self.inst_lbl = MakeWidget.make_label(self.parent, "Please login to continue")
# creating labels and entries for user name and password
self.user_name = MakeWidget.make_entry(self.parent, caption="User Name:")
self.pwd = MakeWidget.make_entry(self.parent, caption="User Password:", show="*")
# create a login button
login_btn = MakeWidget.make_button(self.parent, Verification.verify_user(self.user_name, self.pwd, self.inst_lbl), "Login")
def main():
top = Tk()
app = LoginFrame(top)
top.mainloop()
if __name__ == '__main__':
main()
# verification.py
from tkinter import *
class Verification:
# verify user name and password
#----------------------------------------------------------------------
#staticmethod
def verify_user(user_name, pwd, inst_lbl):
"""verify users"""
if user_name.get() == "admin" and pwd.get() == "123":
inst_lbl.configure(text="User verified")
else:
inst_lbl.configure(text="Access denied. Invalid username or password")
# make_widget.py
from tkinter import *
class MakeWidget(Frame):
def __init__(self, parent):
super(MakeWidget, self).__init__()
self.parent = parent
# create a button widget
#----------------------------------------------------------------------
#staticmethod
def make_button(parent, command, caption=NONE, side=TOP, width=0, **options):
"""make a button"""
btn = Button(parent, text=caption, command=command)
if side is not TOP:
btn.pack(side=side)
else:
btn.pack()
return btn
# create a label widget
#staticmethod
def make_label(parent, caption=NONE, side=TOP, **options):
label = Label(parent, text=caption, **options)
if side is not TOP:
label.pack(side=side)
else:
label.pack()
return label
# create a entry widget
#staticmethod
def make_entry(parent, caption=NONE, side=TOP, width=0, **options):
MakeWidget.make_label(parent, caption, side)
entry = Entry(parent, **options)
if width:
entry.config(width=width)
if side is not TOP:
entry.pack(side=side)
else:
entry.pack()
return entry
Now, I expected the inst_lbl in LoginFrame to be able to configure and display the new text based on the user_name and pwd, but inst_lbl didn't change the text (no error was generated). So how to resolve the issue.
The problem is this line:
login_btn = MakeWidget.make_button(self.parent, Verification.verify_user(self.user_name, self.pwd, self.inst_lbl), "Login")
You are immediately calling Verification.verify_user and assigning the result so the button command. You must pass in a reference to a function, not the result of a function.
See this answer to a similar question: https://stackoverflow.com/a/5771787/7432
I have seen many explanations of how to turn an enabled button disabled but not when classes are involved. The error here is in the line 'button_1.config...' and the error message is that button_1 is not defined. I think this is because it is in a different method but im not sure how to disable a button from a different method. any help is appreciated.
from tkinter import *
class menu:
def __init__(self, master):
self.master = master
button_1 = Button(self.master, text = 'test', command = self.correct).pack()
def correct(self):
button_1.config(state = DISABLED)
def window():
root = Tk()
menu(root)
root.mainloop()
if __name__ == '__main__':
window()
The button needs to be an instance variable, if you're accessing it between methods in the class. Just add self. in front of it. It's also going to need to be packed on a separate line, otherwise the instance variable self.button_1 will return None:
class menu:
def __init__(self, master):
self.master = master
self.button_1 = Button(self.master, text = 'test', command = self.correct)
self.button_1.pack()
def correct(self):
self.button_1.config(state = DISABLED)
I want to create a popup message box which prompts user to enter an input. I have this method inside a class. I am basing my code on this guide by java2s.
class MyDialog:
def __init__(self, parent):
top = self.top = Toplevel(parent)
Label(top, text="Value").pack()
self.e = Entry(top)
self.e.pack(padx=5)
b = Button(top, text="OK", command=self.ok)
b.pack(pady=5)
def ok(self):
print "value is", self.e.get()
self.top.destroy()
root = Tk()
d = MyDialog(root)
root.wait_window(d.top)
But in this, top = self.top = Toplevel(parent) doesn't work for me.
I have a mockup of what I am trying to accomplish.
My program structure looks something like this:
class MainUI:
def__int__(self):
...
self.initUI()
def initUI(self):
.......
Popup = Button(self, text="Enter Value", command=self.showPopup)
def showPopup(self):
#create the popup with an Entry here
How can I create a message box in Python which accepts user input?
I'm a little confused about your two different blocks of code. Just addressing the first block of code, nothing happens because you never enter the mainloop. To do that, you need to call root.mainloop(). The typical way of doing this is to add a button to root widget and bind a callback function to the Button (which includes d=MyDialog() and root.wait_window(d.top))
Here's some basic code which I hope does what you want ...
from Tkinter import *
import sys
class popupWindow(object):
def __init__(self,master):
top=self.top=Toplevel(master)
self.l=Label(top,text="Hello World")
self.l.pack()
self.e=Entry(top)
self.e.pack()
self.b=Button(top,text='Ok',command=self.cleanup)
self.b.pack()
def cleanup(self):
self.value=self.e.get()
self.top.destroy()
class mainWindow(object):
def __init__(self,master):
self.master=master
self.b=Button(master,text="click me!",command=self.popup)
self.b.pack()
self.b2=Button(master,text="print value",command=lambda: sys.stdout.write(self.entryValue()+'\n'))
self.b2.pack()
def popup(self):
self.w=popupWindow(self.master)
self.b["state"] = "disabled"
self.master.wait_window(self.w.top)
self.b["state"] = "normal"
def entryValue(self):
return self.w.value
if __name__ == "__main__":
root=Tk()
m=mainWindow(root)
root.mainloop()
I get the value from the popupWindow and use it in the main program (take a look at the lambda function associated with b2).
Main window:
"Click me" window:
Main window while "click me" is open:
import tkinter as tk
from tkinter import simpledialog
ROOT = tk.Tk()
ROOT.withdraw()
# the input dialog
USER_INP = simpledialog.askstring(title="Test",
prompt="What's your Name?:")
# check it out
print("Hello", USER_INP)
Enjoy ...
I did it in Tkinter without any classes. I created a function that starts a new window.
popup.Tk()
popup.mainloop()
In that window there is an Entry field from where I get the text with a variable which value is: entry.get()
Then you can use that variable for whatever you need and it will take the text from that Entry field.
I just tried this:
def get_me():
s = simpledialog.askstring("input string", "please input your added text")
Source: https://www.youtube.com/watch?v=43vzP1FyAF8