from Tkinter import *
import random
menu = Tk()
subpage = Tk()
entry_values = []
population_values = []
startUpPage = Tk()
def main_menu(window):
window.destroy()
global menu
menu = Tk()
frame1 = Frame(menu)
menu.resizable(width=FALSE, height=FALSE)
button0 = Button(menu, text="Set Generation Zero Values", command=sub_menu(menu))
button1 = Button(menu, text="Display Generation Zero Values")
button2 = Button(menu, text="Run Model")
button3 = Button(menu, text="Export Data")
button4 = Button(menu, text="Exit Program", command=menu.destroy)
button0.grid(row=0, column=0, sticky=W)
button1.grid(row=2, column=0, sticky=W)
button2.grid(row=3, column=0, sticky=W)
button3.grid(row=4, column=0, sticky=W)
button4.grid(row=5, column=0, sticky=W)
menu.mainloop()
def sub_menu(window):
global subpage
window.destroy()
subpage = Tk()
subpage.resizable(width=FALSE, height=FALSE)
#defining sub page items
button5 = Button(subpage, text="Save Generation Data",command = main_menu(subpage))
juveniles_label0 = Label(subpage,text="Juveniles")
adults_label1 = Label(subpage,text="Adults")
seniles_label2 = Label(subpage,text="Seniles")
population_label3 = Label(subpage,text="Popultation")
survival_rate_label4 = Label(subpage,text="Survival Rate (Between 0 and 1)")
entry0 = Entry(subpage)
entry1 = Entry(subpage)
entry2 = Entry(subpage)
entry3 = Entry(subpage)
entry4 = Entry(subpage)
entry5 = Entry(subpage)
button4.grid(row=1, column= 6, sticky=E)
juveniles_label0.grid(row=0, column=1)
adults_label1.grid(row=0, column=2)
seniles_label2.grid(row=0, column=3)
population_label3.grid(row=1, column=0)
survival_rate_label4.grid(row=2, column=0)
entry0.grid(row=1, column=1)
entry1.grid(row=1, column=2)
entry2.grid(row=1, column=3)
entry3.grid(row=2, column=1)
entry4.grid(row=2, column=2)
entry5.grid(row=2, column=3)
#add entry 6 7 8
subpage.mainloop()
main_menu(subpage)
main_menu(startUpPage)
I'm very new to coding and stackoverflow. I am trying to create a GUI that has a main page which will be opened first and a sub page which will be opened by clicking a button which will be stored in the main page. my issue is that I have no clue why it isn't opening my main page. my thought is that it is something to do with the .destroy() or something similar. any help would be much appreciated.
As a general rule, you should create exactly one instance of Tk for the life of your program. That is how Tkinter is designed to be used. You can break this rule when you understand the reasoning behind it, though there are very few good reasons to break the rule.
The simplest solution is to implement your main menu and your sub menu as frames, which you've already done. To switch between them you can simply destroy one and (re)create the other, or create them all ahead of time and then remove one and show the other.
For example, the following example shows how you would create them ahead of time and simply swap them out. The key is that each function needs to return the frame, which is saved in a dictionary. The dictionary is used to map symbolic names (eg: "main", "sub", etc) to the actual frames.
def main_menu(root):
menu = Frame(root)
button0 = Button(menu, text="Set Generation Zero Values",
command=lambda: switch_page("sub"))
...
return menu
def sub_menu(root):
subpage = Frame(root)
button5 = Button(subpage, text="Save Generation Data",
command = lambda: switch_page("main"))
...
return subpage
def switch_page(page_name):
slaves = root.pack_slaves()
if slaves:
# this assumes there is only one slave in the master
slaves[0].pack_forget()
pages[page_name].pack(fill="both", expand=True)
root = Tk()
pages = {
"main": main_menu(root),
"sub": sub_menu(root),
...
}
switch_page("main")
root.mainloop()
For a more complex object-oriented approach see Switch between two frames in tkinter
heres some code that does what you want.. make a window, destroy it when button is clicked and then make a new window...
from Tkinter import *
import random
def main_menu():
global root
root = Tk()
b = Button(root,text='our text button',command = next_page)
b.pack()
def next_page():
global root,parent
parent = Tk()
root.destroy()
new_b = Button(parent,text = 'new Button',command=print_something)
new_b.pack()
def print_something():
print('clicked')
main_menu()
root.mainloop()
parent.mainloop()
ps. ive done this in python3 so keep that in mind though it wouldnt be a problem in my opinion
Related
I am trying to build a small GUI for my D&D character but my second attack button does not appear.
Tried to make it read as easy as possible. Its my second try on programming, I find the Tkinter really difficult to work with :(
Written in Python 3:
# Tkinter_buildframe #
root = tk.Tk()
frame = tk.Frame(root)
frame.pack( side = TOP )
frame.pack()
root.geometry("300x200")
# This is the charicter stats #
w = Label(root, text="""
charisma modifier = 4
spellcast attack_bonus = 7
""", font="12")
w.pack()
# Quit_button #
button = tk.Button(frame,
text="QUIT",
fg="red",
command=quit)
button.pack(side=tk.BOTTOM)
# Attack1 eldritch_blast_with_hex #
slogan = tk.Button(frame,
text="Eldritch Blast with Hex",
command=eldritch_blast_with_hex)
slogan.pack(side=tk.LEFT)
root.mainloop()
def popupmsg(msg):
popup = tk.Tk()
popup.wm_title("Eldritch Blast with Hex")
label = ttk.Label(popup, text=msg, font=NORM_FONT)
label.pack(side="top", fill="x", pady=10)
B1 = ttk.Button(popup, text="Okay", command = popup.destroy)
B1.pack()
popup.mainloop()
# Attack2 eldritch_blast_without_hex#
slogan = tk.Button(frame,
text="Eldritch Blast without Hex",
command=eldritch_blast_without_hex)
slogan.pack(side=tk.LEFT)
root.mainloop()
def popupmsg(msg):
popup = tk.Tk()
popup.wm_title("Eldritch Blast without Hex")
label = ttk.Label(popup, text=msg, font=NORM_FONT)
label.pack(side="top", fill="x", pady=10)
B1 = ttk.Button(popup, text="Okay", command = popup.destroy)
B1.pack()
You have multiple root.mainloop() in the code, it just pauses the code from continuing the execution of rest of the code. So remove one of the root.mainloop() and place one at the end of the code.
When button 1 is pressed I want this program to say word 'hi' and when button 2 is pressed I want it to say 'goodbye' in the same spot as it says hi and override what button 1 produced. However, it doesn't override it just merges the 2 labels together. What is a good way to prevent this from happening while making sure both labels appear in the same spot?
from tkinter import *
root = Tk()
def press():
word = Label(root, text='hi')
word.grid(row=0, column=1)
def press_2():
word_2 = Label(root, text='goodbye')
word_2.grid(row=0, column=1)
button_1 = Button(root, text=1, command=press)
button_2 = Button(root, text=2, command=press_2)
button_1.grid(row=0, column=0)
button_2.grid(row=1, column=0)
root.mainloop()
You just need to use one Label and keep changing its text. We can do this efficiently by using a lambda for the widget command. As stated by #Bryan Oakley, using a StringVar adds unnecessary overhead. This is a slightly modified version from my original. In this version we use the config method of the widget to set the text. As a bonus the code is formatted with a class structure. Using a procedural style with tkinter eventually turns into a big mess. Considering the required imports are minimal, we import them directly and do not need to prefix every widget.
from tkinter import Tk, Button, Label
class Application(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
lbl = Label(self, text='hi')
lbl.grid(row=0, column=1)
btn1 = Button(self, text='1',)
btn1.config(command=lambda m='hi': lbl.config(text=m))
btn1.grid(row=0, column=0)
btn2 = Button(self, text='2')
btn2.config(command=lambda m='goodbye': lbl.config(text=m))
btn2.grid(row=1, column=0)
if __name__ == "__main__":
app = Application()
app.minsize(100, 50)
app.title("My Application")
app.mainloop()
from tkinter import *
root = Tk()
def press():
label['text'] = "hi"
def press_2():
label['text'] = "goodbye"
label = Label(root)
label.grid(row=0, column=1)
button_1 = Button(root, text=1, command=press)
button_2 = Button(root, text=2, command=press_2)
button_1.grid(row=0, column=0)
button_2.grid(row=1, column=0)
root.mainloop()
Put it on the windows firstly,Then change the text config.
A better way to do so would be by using config() method of widgets. But here i dont know if its gonna help out much. But give it a try
from tkinter import *
root = Tk()
def press():
word.config(text='hi')
def press_2():
word.config(text='goodbye')
button_1 = Button(root, text=1, command=press)
button_2 = Button(root, text=2, command=press_2)
button_1.grid(row=0, column=0)
button_2.grid(row=1, column=0)
word = Label(root, text='') #creating a blank label to edit later on in functions
word.grid(row=0, column=1)
root.mainloop()
I have created a chat application, in which i use ListBox for showing the chat history. It looks good until I enter a long sting which goes beyond the screen. Is there a way to break the string and show in new line or any other way to show the complete string. I'm new to Tkinter and im not aware of many widgets available.
Here is my sample code
from tkinter import *
class Actions:
def chatUpdate(chat):
chatlist.insert(Actions.chatLast,chat)
Actions.chatLast=Actions.chatLast+1
chatlist.pack( side=LEFT, fill=BOTH)
chatBox.config(command=chatlist.yview)
def callUpdater():
txt=textBox.get()
text_text.set("")
Actions.chatUpdate(txt)
root = Tk()
root.title("Chat App")
frame1 = Frame(root, bd=4)
frame1.pack(side=TOP)
frame2 = Frame(root, bd=4)
frame2.pack(side=TOP)
frame3 = Frame(root, bd=4)
frame3.pack(side=TOP)
# chat box
chatBox = Scrollbar(frame1)
chatBox.pack(side=RIGHT, fill=Y)
chatlist = Listbox(frame1, yscrollcommand = chatBox.set, width=50)
Actions.chatLast=0
Actions.chatUpdate(" ")
# text box
textView = Label(frame2, text="Input: ")
textView.pack(side=LEFT)
text_text = StringVar()
textBox = Entry(frame2, textvariable=text_text, bd=0, width=40, bg="pink")
textBox.pack(side=RIGHT)
# send button
button = Button(frame3, text="Send", fg="black", command=callUpdater)
button.pack(side=TOP)
root.mainloop()
You can replace the Listbox by a Text widget in 'disabled' mode which automatically wraps long lines. You will just need to put the widget back in 'normal' mode each time you insert text:
from tkinter import *
def callUpdater():
text = textBox.get()
textBox.delete(0, 'end')
chat.configure(state='normal')
chat.insert('end', text + '\n')
chat.configure(state='disabled')
root = Tk()
chatBox = Scrollbar(root)
chat = Text(root, wrap='word', state='disabled', width=50,
yscrollcommand=chatBox.set)
chatBox.configure(command=chat.yview)
chat.grid(row=0, columnspan=2, sticky='ewns')
chatBox.grid(row=0, column=2, sticky='ns')
Label(root, text="Input: ").grid(row=1, column=0)
textBox = Entry(root, bd=0, width=40, bg="pink")
textBox.grid(row=1, column=1)
Button(root, text="Send", command=callUpdater).grid(row=2, columnspan=2)
root.mainloop()
By the way, both the Listbox and Text widgets support the index 'end' so you don't have to keep track of how many lines you have inserted.
I started using TK in python to build a graphical interface for my program.
I'm not able to fix 2 issues concerning (1) the position of a button in the window and (2) use a value of a radiobutton inside a fucntion.
This is my current code:
root = tk.Tk()
root.title("START")
root.geometry("500x200+500+200")
v = tk.IntVar()
v.set(0) # initializing the choice
my_choise = [
("Basic",1),
("Advanced",2),
('Extreme',3)
]
def ShowChoice():
print(v.get())
tk.Label(root,
text="""Choose your configuration:""",
justify = tk.LEFT,
padx = 20).pack()
val = 0
for val, choise in enumerate(my_choise):
tk.Radiobutton(root,text=choise,padx = 20,variable=v,command=ShowChoice,value=val).pack(anchor=tk.W)
def star_program(value):
os.system("ifconfig")
def open_comments_file():
os.system("gedit /home/user/Desktop/comments.txt")
def open_links_file():
os.system("gedit /home/user/Desktop/links.txt")
frame = tk.Frame(root)
frame.pack()
open_file_c = tk.Button(frame,
text="Comments",
command=open_comments_file)
open_file_f = tk.Button(frame,
text="Links",
command=open_links_file)
button = tk.Button(frame,
text="Start",
command=star_program(v.get()))
button.pack(side=tk.LEFT)
open_file_f.pack(side=tk.LEFT)
open_file_c.pack(side=tk.LEFT)
slogan = tk.Button(frame,
text="Cancel",
command=quit)
slogan.pack(side=tk.LEFT)
root.mainloop()
I would like that the buttons "Links" and "Comments" were positioned below the radiobutton, one below the other. Now, all buttons are in line, but I would like to have "start" and "cancel" at the bottom of my window.
Then I tried to use the value of the radiobutton (choice) inside the star_program function. It does not work. My idea is, based on the choice selected in the radiobutton, perform different actions when I click the button "start":
def star_program(value):
if value == 0:
os.system("ifconfig")
else:
print "Goodbye"
In addition, concerning "start" button, I have a strange behavior. The program runs "ifconfig" command also if I don't click on "start". And If I click "start" it does not perform any action.
Any suggestion?
Thanks!!!
i'm assuming this is more like what you're after:
root = tk.Tk()
root.title("START")
root.geometry("500x200+500+200")
v = tk.IntVar()
v.set(0) # initializing the choice
my_choise = [
("Basic",1),
("Advanced",2),
('Extreme',3)
]
def ShowChoice():
print(v.get())
tk.Label(root,
text="""Choose your configuration:""",
justify = tk.LEFT,
padx = 20).grid(column=1, row=0, sticky="nesw") # use grid instead of pack
root.grid_columnconfigure(1, weight=1)
val = 0
for val, choise in enumerate(my_choise):
tk.Radiobutton(root,text=choise,padx = 20,variable=v,command=ShowChoice,value=val).grid(column=1, row=val+1, sticky="nw")
def star_program(value):
os.system("ifconfig")
def open_comments_file():
os.system("gedit /home/user/Desktop/comments.txt")
def open_links_file():
os.system("gedit /home/user/Desktop/links.txt")
frame = tk.Frame(root)
frame.grid(column=1, row=4, sticky="nesw")
open_file_c = tk.Button(frame,
text="Comments",
command=open_comments_file)
open_file_f = tk.Button(frame,
text="Links",
command=open_links_file)
button = tk.Button(frame,
text="Start",
command=lambda: star_program(v.get()))
# use lambda to create an anonymous function to be called when button pushed,
needed for functions where arguments are required
button.grid(column=2, row=3, sticky="nesw")
open_file_f.grid(column=1, row=1, sticky="nesw")
open_file_c.grid(column=1, row=2, sticky="nesw")
slogan = tk.Button(frame,
text="Cancel",
command=quit)
slogan.grid(column=4, row=3, sticky="nesw")
root.mainloop()
The problem with the "start" button is due to the function definition.
This is the right code that does not trigger any action if you don't click the button:
button = tk.Button(frame,
text="Start",
command=star_program)
I have been trying to make an entry box for people to enter their names, as part of a "registration" for a mini-program that I am doing, but the result is that an entry box doesn't appear at all.
Here is the section of the code that is troubling me:
def win2(self):
# this is the child window
board = Toplevel()
board.title("Sign up")
board.focus_set()
board.grab_set()
userVar = StringVar()
userVar.set('Username')
square1Label = Label(board,textvariable=userVar)
square1Label.grid(row=0, column=7)
userEnt=Entry(self)
userEnt.grid(row=1, column=7)
s2Var = StringVar()
s2Var.set('First Name')
square2Label = Label(board,textvariable=s2Var)
square2Label.grid(row=1, column=7)
leaveButton = Button(board, text="Quit", command=board.destroy)
leaveButton.grid(row=1, column=1, sticky='nw')
board.wait_window(board)
And here is the coding in full, minus the importing of Tkinter and the mainloop:
class Application(Frame):
"""GUI Application for making Baakibook"""
def __init__(self, parent):
"""Initialize the frame"""
Frame.__init__(self, parent, bg="light blue")
self.win1()
# different windows
def win1(self):
# this is the main/root window
signupButton = Button(root, text="Sign up", command=self.win2)
signupButton.grid(row=9, column=7)
loginButton = Button(root, text="Log in", command=self.win3)
loginButton.grid(row=10, column=7)
leaveButton = Button(root, text="Quit", command=root.destroy)
leaveButton.grid(row=1, column=1, sticky='nw')
b1Var = StringVar()
b2Var = StringVar()
b1Var.set('b1')
b2Var.set('b2')
box1Label = Label(root,textvariable=b1Var,width=12)
box1Label.grid(row=3, column=2)
box2Label = Label(root,textvariable=b2Var,width=12)
box2Label.grid(row=3, column=3)
root.mainloop()
def win2(self):
# this is the child window
board = Toplevel()
board.title("Sign up")
board.focus_set()
board.grab_set()
userVar = StringVar()
userVar.set('Username')
square1Label = Label(board,textvariable=userVar)
square1Label.grid(row=0, column=7)
userEnt=Entry(self)
userEnt.grid(row=1, column=7)
s2Var = StringVar()
s2Var.set('First Name')
square2Label = Label(board,textvariable=s2Var)
square2Label.grid(row=1, column=7)
leaveButton = Button(board, text="Quit", command=board.destroy)
leaveButton.grid(row=1, column=1, sticky='nw')
board.wait_window(board)
def win3(self):
# this is the child window
board = Toplevel()
board.title("Login User")
board.focus_set()
board.grab_set()
s1Var = StringVar()
s2Var = StringVar()
s1Var.set("s1")
s2Var.set("s2")
square1Label = Label(board,textvariable=s1Var)
square1Label.grid(row=0, column=7)
square2Label = Label(board,textvariable=s2Var)
square2Label.grid(row=0, column=6)
leaveButton = Button(board, text="Quit", command=board.destroy)
leaveButton.grid(row=1, column=1, sticky='nw')
board.wait_window(board)
The first argument you use to create the widget is what parent you want it assigned to. So, this line:
userEnt=Entry(self)
userEnt.grid(row=1, column=7)
makes the Entry widget in the Frame that you created in __init__. If you want it to appear in the Toplevel, create it like the other widgets you made in that method, ie:
userEnt=Entry(board)
userEnt.grid(row=1, column=7)
Also, your grid in the Toplevel doesn't make sense right now and a couple of your widgets will appear stacked until you change it.