from tkinter import *
from tkinter import messagebox
w = Tk()
w.geometry('200x250')
def buttons():
r1 = Tk()
r1.geometry('200x250')
r1.mainloop()
t= Label(text = "MIB",font =("Arial", 49))
t.pack(side = TOP)
e = Label(text = "Email")
e1 =Entry()
e.pack()
e1.pack()
p = Label(text = "Password")
p.pack()
p1 = Entry()
p1.pack()
b= Button(text = "SIGN UP", command = buttons)
b.pack()
w.mainloop()
How I can display the entry widget after clicking submit button in the new window in tkinter python? I tried defining under button function but it is showing me the window but not the widgets. The widgets are only displayed on the first window after clicking submit button.
TK() is the root window, so it needs to be called only once. After that, to open another window, you need to use tkinter.Toplevel(). Below is the code I put labelexample in a new window.
from tkinter import *
from tkinter import messagebox
w = Tk()
w.geometry('200x250')
def buttons():
r1 = Toplevel(w)
r1.geometry('200x250')
labelexample = Label(r1, text = 'GOOD')
labelexample.pack()
t= Label(text = "MIB",font =("Arial", 49))
t.pack(side = TOP)
e = Label(text = "Email")
e1 =Entry()
e.pack()
e1.pack()
p = Label(text = "Password")
p.pack()
p1 = Entry()
p1.pack()
b= Button(text = "SIGN UP", command = buttons)
b.pack()
w.mainloop()
Related
Im trying to create a Python Tkinter programme that resets the window based on a radio button selection. So that the user sees the correct input field(s) for the radio button selected. In the example below the user select a radiobutton to enter a band name (e.g. Duran Duran) or artist name in to 2 fields (e.g. Michael Kiwanuka).
The problem is that the tk windows just keep spawning new windows.
What I really want to understand is how can I create a window, add some widgets and then change the widgets depending on the radio button choice.
My code is:
from tkinter import *
window = Tk()
window.title("My Project")
# parent_window.geometry("width_size x height_size + x_position + y_position")
window.minsize(width=850, height=550)
window.configure(bg="#bcbcbc")
def make_window():
window = Tk()
window.title("My Project")
# parent_window.geometry("width_size x height_size + x_position + y_position")
window.minsize(width=850, height=550)
window.configure(bg="#bcbcbc")
radio_state = IntVar()
radiobutton1 = Radiobutton(text="Artist is a Band", value=1, variable=radio_state, command=artist_band)
radiobutton2 = Radiobutton(text="Artist is a Person", value=2, variable=radio_state, command=artist_person)
radiobutton1.pack()
radiobutton2.pack()
def reset_all():
window.destroy()
make_window()
def artist_band():
print("Artist is a Band")
reset_all()
make_window()
inp_band_name = Entry(borderwidth=2, background="white", width=40, fg="red")
inp_band_name.insert(END, string="")
inp_band_name.pack()
def artist_person():
print("Artist is a Person")
reset_all()
make_window()
inp_first_name = Entry(borderwidth=2, background="white", width=25, )
inp_first_name.insert(END, string="")
inp_first_name.pack()
inp_second_name = Entry(borderwidth=2, background="white", width=25, )
inp_second_name.insert(END, string="")
inp_second_name.pack()
radio_state = IntVar()
radiobutton1 = Radiobutton(text="Artist is a Band", value=1, variable=radio_state, command=artist_band)
radiobutton2 = Radiobutton(text="Artist is a Person", value=2, variable=radio_state, command=artist_person)
radiobutton1.pack()
radiobutton2.pack()
window.mainloop()
window = Tk() should be the only one in the program. It can be minimized or hidden, but when it is closed, the program also ends. Child windows are created using Toplevel().
from tkinter import *
def make_window(text):
win = Toplevel()
win.title(text)
# parent_window.geometry("width_size x height_size + x_position + y_position")
win.minsize(width=850, height=550)
win.configure(bg="#bcbcbc")
name = Entry(win, borderwidth=2, background="white", width=40, fg="red")
name.insert(END, string=text)
name.pack()
window.iconify()
def reset_all():
window.destroy()
make_window()
def artist_band():
print("Artist is a Band")
make_window('Artist is a Band')
def artist_person():
print("Artist is a Person")
make_window('Artist is a Person')
window = Tk()
window.title("My Project")
# parent_window.geometry("width_size x height_size + x_position + y_position")
window.minsize(width=850, height=550)
window.configure(bg="#bcbcbc")
radio_state = IntVar()
radiobutton1 = Radiobutton(window, text="Artist is a Band", value=1, variable=radio_state, command=artist_band)
radiobutton2 = Radiobutton(window, text="Artist is a Person", value=2, variable=radio_state, command=artist_person)
radiobutton1.pack()
radiobutton2.pack()
window.mainloop()
Windows and Dialogs
Just a simple example of a problem I experienced:
from tkinter import *
root = Tk()
frame = Frame(root)
label = Label(frame, text = "Hey")
label.pack()
def packframe():
frame.pack()
def destroyframe():
frame.destroy()
pack_button = Button(root, text = "pack", command = packframe)
pack_button.pack()
des_button = Button(root, text = "destroy", command = destroyframe)
des_button.pack()
Once I press the destroy button, I cannot pack it back again on the screen with the pack_button. Im not sure why is it so, but I would appreciate both explanaition and a solution
I am trying to display some text after a button is pressed but all I seem to be able to do make it so that text is displayed before the button is pressed or not at all.
here is my code so far:
import tkinter
def label1():
label2 = tkinter.Label(window1, text = "correct")
label.pack()
def Window2():
window1 = tkinter.Tk()
window1.title("start")
label = tkinter.Label(window1, text= "how do you spell this Sh--ld")
label.pack()
points = 0
i = points + 1
button = tkinter.Button(window1, text = "ou", command = label1)
button.pack()
window = tkinter.Tk()
window.title("menu")
button = tkinter.Button(window, text = "start", command = Window2)
button.pack()
I am trying to get the button in the Window2 subroutine to display the text
Here is how you can do it
import tkinter
def label1(root):
label = tkinter.Label(root, text = "correct")
label.pack()
def Window2():
window1 = tkinter.Tk()
window1.title("start")
label = tkinter.Label(window1, text= "how do you spell this Sh--ld")
label.pack()
points = 0
i = points + 1
button = tkinter.Button(window1, text = "ou", command = lambda root = window1: label1(root))
button.pack()
window = tkinter.Tk()
window.title("menu")
button = tkinter.Button(window, text = "start", command = Window2)
button.pack()
window.mainloop()
Why it does not print the string values I have entered into the textbox in the newwindow?
from tkinter import *
def newwindow():
newwindow = Tk()
newwindow.title('Sign Up')
newwindow.geometry('200x400')
def sign_done():
david = a.get()
javed = b.get()
lbee = Label(newwindow, text=david).pack()
baeee = Label(newwindow, text=javed).pack()
a = StringVar()
b = StringVar()
user = Entry(newwindow, textvariable=a).pack()
pword = Entry(newwindow, textvariable=b).pack()
done = Button(newwindow, text='done now', command=sign_done).pack()
newwindow.mainloop()
root = Tk()
root.title('Gulmeena')
root.geometry("500x200")
button = Button(root, text='Go', command=newwindow).pack()
root.mainloop()
Please do not use Classes
Use Tk only to create main window. To create any other window use Toplevel. And use only one mainwindow().
var = Widget(...).pack() assigns None to var because pack()/grid()/place() returns None. You have to do it in two lines.
var = Widget(...)
var.pack().
If you don't need var then you can do
Widget(...).pack()
To make code more readable
I use import tkinter as tk to show I use tk.Button, not ttk.Button nor my own classButton`
I use name user_var and password_var which means something
I put all functions at the beginning - even inside newwindow
Code:
import tkinter as tk
def newwindow():
def sign_done():
david = user_var.get()
javed = password_var.get()
tk.Label(newwindow, text=david).pack()
tk.Label(newwindow, text=javed).pack()
newwindow = tk.Toplevel()
newwindow.title('Sign Up')
newwindow.geometry('200x400')
user_var = tk.StringVar()
password_var = tk.StringVar()
user = tk.Entry(newwindow, textvariable=user_var)
user.pack()
pword = tk.Entry(newwindow, textvariable=password_var)
pword.pack()
tk.Button(newwindow, text='done now', command=sign_done).pack()
root = tk.Tk()
root.title('Gulmeena')
root.geometry("500x200")
tk.Button(root, text='Go', command=newwindow).pack()
root.mainloop()
You can do the same without StringVars
import tkinter as tk
def newwindow():
def sign_done():
david = user.get()
javed = pword.get()
tk.Label(newwindow, text=david).pack()
tk.Label(newwindow, text=javed).pack()
newwindow = tk.Toplevel()
newwindow.title('Sign Up')
newwindow.geometry('200x400')
user = tk.Entry(newwindow)
user.pack()
pword = tk.Entry(newwindow)
pword.pack()
tk.Button(newwindow, text='done now', command=sign_done).pack()
root = tk.Tk()
root.title('Gulmeena')
root.geometry("500x200")
tk.Button(root, text='Go', command=newwindow).pack()
root.mainloop()
This is program which I made for now, but I have problem...
how can I make when I click on button1 that then opens new window
import sys
from tkinter import *
import tkinter as tk
def mhello1():
mlabel = Label(mGui, text='A1').pack()
def mhello2():
mlabel = Label(mGui, text='A2').pack()
def mhello3():
mlabel = Label(mGui, text='A3').pack()
def mhello4():
mlabel
return
def mAbout():
messagebox.showinfo(title="About",message="program")
return
def mQuit():
mExit = messagebox.askyesno(title="Quit",message="y/n")
if mExit > 0:
mGui.destroy()
return
mGui = Tk()
mGui.geometry('450x450+200+200')
mGui.title('program')
mGui.configure(bg='gray')
mlabel = Label(text='option:',fg='red',bg = 'blue').pack()
mbutton1 = Button(mGui,text ='Button1',command = mhello1, height=5, width=20).pack()
mbutton2 = Button(mGui,text ='Button2',command = mhello2, height=5, width=20).pack()
mbutton3 = Button(mGui,text ='Button3',command = mhello3, height=5, width=20).pack()
mbutton4 = Button(mGui,text ='Button4',command = mhello4, height=5, width=20).pack()
mlabel2 = Label(text='activity:',fg='red',bg = 'blue').pack()
menubar=Menu(mGui)
filemenu = Menu(menubar, tearoff = 0)
filemenu.add_command(label="qwer")
filemenu.add_command(label="quit",command = mQuit)
menubar.add_cascade(label="more options",menu=filemenu)
helpmenu = Menu(menubar, tearoff = 0)
helpmenu.add_command(label="Help Docs")
helpmenu.add_command(label="About", command = mAbout)
menubar.add_cascade(label="help",menu=helpmenu)
mGui.config(menu=menubar)
mGui.mainloop()
I try this program but it doesn't work:
Python 3 and tkinter opening new window by clicking the button
is there a way that I don't use tkinter toplevel?
Tnx a lot :)
Since you should create only one root window, you have to use a Toplevel to open a new one.
def mhello1():
toplevel = Toplevel()
toplevel.title('Another window')
toplevel.focus_set()
if you wanna use messagebox use those line below
from tkinter import *
from tkinter import messagebox