.
from tkinter import *
import tkinter.messagebox
class sign:
def __init__(self):
self.gui=Tk()
self.gui.geometry("800x450")
self.gui.title("Quiz")
self.signup_form()
def signup_form(self):
global username
global password
self.username = StringVar()
self.password = StringVar()
Label(self.gui, text="").pack()
Label(self.gui, text="Username :", fg="black", font=('arial', 12, 'bold')).pack()
Entry(self.gui, textvariable=self.username).pack()
Label(self.gui, text="").pack()
Label(self.gui, text="Password :", fg="black", font=('arial', 12, 'bold')).pack()
Entry(self.gui, textvariable=self.password, show="*").pack()
Label(self.gui, text="").pack()
Button(self.gui, text="sign up", bg="white", fg='blue', relief="solid", font=('arial', 12, 'bold'),command=self.msg).pack()
Label(self.gui, text="")
def msg(self):
tkinter.messagebox.showinfo("Msg",self.username)
signup=sign()
CodePudding user response:
Try this:
from tkinter import *
import tkinter.messagebox
class sign:
def __init__(self):
self.gui=Tk()
self.gui.geometry("800x450")
self.gui.title("Quiz")
self.signup_form()
self.gui.mainloop()
def signup_form(self):
global username
global password
self.username = StringVar()
self.password = StringVar()
Label(self.gui, text="").pack()
Label(self.gui, text="Username :", fg="black", font=('arial', 12, 'bold')).pack()
Entry(self.gui, textvariable=self.username).pack()
Label(self.gui, text="").pack()
Label(self.gui, text="Password :", fg="black", font=('arial', 12, 'bold')).pack()
Entry(self.gui, textvariable=self.password, show="*").pack()
Label(self.gui, text="").pack()
Button(self.gui, text="sign up", bg="white", fg='blue', relief="solid", font=('arial', 12, 'bold'),command=self.msg).pack()
Label(self.gui, text="")
def msg(self):
tkinter.messagebox.showinfo("Msg",self.username.get())
signup=sign()
CodePudding user response:
I am going to answer your question and improve your code a bit (with explanation)
sections:
- problem solving
- classes and tkinter
I assume you want the code to run like this:
- start gui.
- input username and password.
- show username in messagebox.
- run program
problem solving
I think you mean with "not working properly" that it outputs "PY_VAR0". What happens here is that you're not actually extracting the input of the textbox, but the name that python gives it to identify it. To solve this just use the get()
function.
def msg(self):
tkinter.messagebox.showinfo("Msg",self.username.get())
classes and tkinter
In tkinter there's something called mainloop()
. that makes sure that the gui gets displayed and the program responds to the users input. If you use classes, you should add a parameter master
in the __init__()
function. then below your class you can specify your master. I will call him root
because it's the commonly used name for a master.
class sign:
def __init__(self,master):
self.gui=master
self.gui.geometry("800x450")
self.gui.title("Quiz")
root = Tk()
signup = sign(root)
root.mainloop()
Tip: if you want to use multiple windows, use toplevels.
This is the complete code:
from tkinter import *
import tkinter.messagebox
class sign:
def __init__(self,master):
self.gui=master
self.gui.geometry("800x450")
self.gui.title("Quiz")
self.signup_form()
def signup_form(self):
self.username = StringVar()
self.password = StringVar()
Label(self.gui, text="").pack()
Label(self.gui, text="Username :", fg="black", font=('arial', 12, 'bold')).pack()
Entry(self.gui, textvariable=self.username).pack()
Label(self.gui, text="").pack()
Label(self.gui, text="Password :", fg="black", font=('arial', 12, 'bold')).pack()
Entry(self.gui, textvariable=self.password, show="*").pack()
Label(self.gui, text="").pack()
Button(self.gui, text="sign up", bg="white", fg='blue', relief="solid", font=('arial', 12, 'bold'),command=self.msg).pack()
Label(self.gui, text="")
def msg(self):
tkinter.messagebox.showinfo("Msg",self.username.get())
username = self.username.get()
password = self.password.get()
username = ""
password = ""
root = Tk()
signup = sign(root)
root.mainloop()
Tip: take a look at this course about the difference between global and local variables