I am trying to make text appear in an entry box in my GUI application when a button is pressed. The aim is that when one button is pressed some defined text appear int he textbook and when another button is pressed the previous entry box is cleared and different text is inserted into the same entry box.
I am new to Python and therefore unsure how to do this? So far I have got three buttons to display different text each buttony in the GUI as text rather than text in separate text boxes. Could someone help please? Here's my code currently:
`# ***** Foreword Code *****
from tkinter import *
from tkinter import ttk
import tkinter.messagebox
def new():
tkinter.messagebox.showinfo('Window Title', 'Well, this is new...')
root = Tk()
root.title("GUI Test Version 2")
root.resizable(False, False)
root.geometry('{}x{}'.format(400, 400))
***** Main Menu *****
menu = Menu(root)
root.config(menu=menu)
subMenu = Menu(menu)
menu.add_cascade(label="File", menu=subMenu)
subMenu.add_command(label="New Experiment...", command=new)
subMenu.add_command(label="New...", command=new)
subMenu.add_separator()
subMenu.add_command(label="Exit", command=root.destroy)
editMenu = Menu(menu)
menu.add_cascade(label="Edit", menu=editMenu)
editMenu.add_command(label="Redo", command=new)
***** Toolbar *****
toolbar = Frame(root, bg="light blue")
toolbar.pack(side=TOP, fill=X)
***** Creating Buttons *****
class App(object):
def __init__(self,root):
self.root = root
self.txt_frm = Frame(self.root, width=900, height=900)
self.txt_frm.pack(fill="both", expand=True)
button1 = Button(self.txt_frm,text="HELLO", command = self.hello_world)
button1.grid(column=0,row=2, padx=2, pady=2)
button2 = Button(self.txt_frm,text="GOODBYE", command = self.goodbye_world)
button2.grid(column=1,row=2, padx=2, pady=2)
button3 = Button(self.txt_frm,text="NEW", command = self.new_world, bg="red",fg="white")
button3.grid(column=2,row=2, padx=2, pady=2)
def hello_world(self):
label = Label(self.txt_frm,text='HELLO WORLD!')
label.grid(column=0,row=3)
def goodbye_world(self):
label = Label(self.txt_frm,text='GOODBYE WORLD!')
label.grid(column=1,row=3)
def new_world(self):
label = Label(self.txt_frm,text='THIS IS A NEW WORLD!')
label.grid(column=2,row=3)
***** Status Bar *****
status = Label(root, text="Preparing to begin...", bd=1, relief=SUNKEN, anchor=W) # bd = bordered, relief = , appear placed in screen, anchor = w (NESW) needs two other properties
status.pack(side=BOTTOM, fill=X)
***** Run Code *****
app = App(root)
root.mainloop()`
The usual way to read and write to an Entry is to use a StringVar as textvariable. Inspect the code below:
from tkinter import *
root = Tk()
root.geometry('300x100')
class App(object):
def __init__(self,root):
self.root = root
self.txt_frm = Frame(self.root, width=900, height=900, bg='khaki')
self.txt_frm.pack(fill="both", expand=True)
button1 = Button(self.txt_frm,text="Hello", command = self.hello_world)
button1.grid(column=0,row=2, padx=2, pady=2)
button2 = Button(self.txt_frm,text="Goodbye", command = self.goodbye_world)
button2.grid(column=1,row=2, padx=2, pady=2)
self.entry_var = StringVar()
entry = Entry(self.txt_frm, textvariable=self.entry_var)
entry.grid(column=0, row=3, columnspan=2, padx=2, pady=2)
def hello_world(self):
self.entry_var.set('Hello')
def goodbye_world(self):
self.entry_var.set('World')
app = App(root)
root.mainloop()
I'm assigning the StringVar self.entry_var to the entry. Then I use the button callback functions to alter the contents of the entry by modifying the self.entry_var.
Make a label in __init__ and use label.config(text=text) to change its text later. Here is an example code:
from tkinter import *
from tkinter import ttk
import tkinter.messagebox
def new():
tkinter.messagebox.showinfo('Window Title', 'Well, this is new...')
root = Tk()
root.title("GUI Test Version 2")
root.resizable(False, False)
root.geometry('{}x{}'.format(400, 400))
menu = Menu(root)
root.config(menu=menu)
subMenu = Menu(menu)
menu.add_cascade(label="File", menu=subMenu)
subMenu.add_command(label="New Experiment...", command=new)
subMenu.add_command(label="New...", command=new)
subMenu.add_separator()
subMenu.add_command(label="Exit", command=root.destroy)
editMenu = Menu(menu)
menu.add_cascade(label="Edit", menu=editMenu)
editMenu.add_command(label="Redo", command=new)
toolbar = Frame(root, bg="light blue")
toolbar.pack(side=TOP, fill=X)
class App(object):
def __init__(self,root):
self.root = root
self.txt_frm = Frame(self.root, width=900, height=900)
self.txt_frm.pack(fill="both", expand=True)
button1 = Button(self.txt_frm,text="HELLO", command = self.hello_world)
button1.grid(column=0,row=2, padx=2, pady=2)
button2 = Button(self.txt_frm,text="GOODBYE", command = self.goodbye_world)
button2.grid(column=1,row=2, padx=2, pady=2)
button3 = Button(self.txt_frm,text="NEW", command = self.new_world, bg="red",fg="white")
button3.grid(column=2,row=2, padx=2, pady=2)
self.label = Label(self.txt_frm,text='')
self.label.grid(column=0,row=3)
def hello_world(self):
self.label.config(text="HELLO WORLD!")
def goodbye_world(self):
self.label.config(text="GOODBYE WORLD!")
def new_world(self):
self.label.config(text="THIS IS A NEW WORLD!")
status = Label(root, text="Preparing to begin...", bd=1, relief=SUNKEN, anchor=W) # bd = bordered, relief = , appear placed in screen, anchor = w (NESW) needs two other properties
status.pack(side=BOTTOM, fill=X)
app = App(root)
root.mainloop()
Related
I want to change this label so I used the widget.config option but it's not working for some reason. Here's my code:
import tkinter as tk
root = tk.Tk()
root.attributes('-fullscreen', True)
exit_button = tk.Button(root, text="Exit", command = root.destroy)
exit_button.place(x=1506, y=0)
def answer():
global main_entry
answer_label.config(main_entry)
frame = tk.Frame(root)
main_entry = tk.Entry(frame, width=100)
main_entry.grid(row=0, column=0)
go_button = tk.Button(frame, text='Go!', width=85, command = answer)
go_button.grid(row=1, column=0)
answer_label = tk.Label(text = "Hey").pack()
frame.place(relx=.5, rely=.5, anchor='center')
root.mainloop()
While using the config function, you have to mention what is it that you want to change.
try:-
answer_label.config(text="Text that you want to be displayed")
Also you have not fetched the value from the Entry widget: For that, you can use:
answer_label.config(text=main_entry.get())
Full code will look like this:
import tkinter as tk
root = tk.Tk()
root.attributes('-fullscreen', True)
exit_button = tk.Button(root, text="Exit", command = root.destroy)
exit_button.place(x=1506, y=0)
def answer():
answer_label.config(text=main_entry.get())
frame = tk.Frame(root)
main_entry = tk.Entry(frame, width=100)
main_entry.grid(row=0, column=0)
go_button = tk.Button(frame, text='Go!', width=85, command = answer)
go_button.grid(row=1, column=0)
answer_label = tk.Label(text = "Hey")
answer_label.pack()
frame.place(relx=.5, rely=.5, anchor='center')
root.mainloop()
This code works. It configures the label to change the text. I don't know what you are trying to achieve with the config(main_entry).
import tkinter as tk
root = tk.Tk()
root.attributes('-fullscreen', True)
exit_button = tk.Button(root, text="Exit", command = root.destroy)
exit_button.place(x=1506, y=0)
def answer():
global main_entry, answer_label
answer_label.config(text="hi")
frame = tk.Frame(root)
main_entry = tk.Entry(frame, width=100)
main_entry.grid(row=0, column=0)
go_button = tk.Button(frame, text='Go!', width=85, command = answer)
go_button.grid(row=1, column=0)
answer_label = tk.Label(text = "Hey")
answer_label.pack()
frame.place(relx=.5, rely=.5, anchor='center')
root.mainloop()
I am having an issue getting text to show on Tkinter buttons. I don't get any errors so im not sure why this is the case, I will post the full working code below if anyone can see any obvius errors.I have never had any problems with button labels before but I am trying a new layout with dropdown tabs so this could be a reason for the issue.
import tkinter as tk
from tkinter import ttk
class ToggledFrame(tk.Frame):
def __init__(self, parent, text="", *args, **options):
tk.Frame.__init__(self, parent, *args, **options)
root.state('zoomed')
root.configure(background='black')
root.title("Stylibleue dashboard")
self.show = tk.IntVar()
self.show.set(0)
self.title_frame = ttk.Frame(self)
self.title_frame.pack(fill="x", expand=1)
ttk.Label(self.title_frame, text=text).pack(side="left", fill="x")
self.toggle_button = ttk.Checkbutton(self.title_frame, width=1, text='+',
command=self.toggle,
variable=self.show, style='Toolbutton')
self.toggle_button.pack(side="left", fill="x", expand=1)
self.sub_frame = tk.Frame(self, relief="sunken", borderwidth=1)
def toggle(self):
if bool(self.show.get()):
self.sub_frame.pack(fill="x", expand=1)
self.toggle_button.configure(text='-')
else:
self.sub_frame.forget()
self.toggle_button.configure(text='+')
def helloCallBack (self):
print ("hello")
if __name__ == "__main__":
root = tk.Tk()
t = ToggledFrame(root, text='Bassin 1', relief="raised")
t.pack(fill="x", anchor="s")
B = ttk.Button(t.sub_frame, text ='Feeder 1',command = quit)
ttk.Button(t.sub_frame).pack( expand=0, pady=2, padx=2, anchor="w")
c = ttk.Button(t.sub_frame, text ="Feeder 2")
ttk.Button(t.sub_frame).pack(expand=0, pady=2, padx=2, anchor="w")
t2 = ToggledFrame(root, text='Bassin 2', relief="raised")
t2.pack(fill="x")
d = ttk.Button(t2.sub_frame, text ='Feeder 1',command = quit)
ttk.Button(t2.sub_frame).pack( expand=0, pady=2, padx=2, anchor="w")
e = ttk.Button(t2.sub_frame, text ="Feeder 2")
ttk.Button(t2.sub_frame).pack(expand=0, pady=2, padx=2, anchor="w")
root.mainloop()
You are creating 8 buttons but only giving a label to four of them, and it’s the ones without a label that you are calling .pack on. The ones with the label are never added to the window.
I'm trying to create a GUI that opens up a new window after pressing a button while destroying the last window. I'm not getting any errors but when I run my program nothing comes up.
from Tkinter import *
def team():
def table():
table = Toplevel(contributers)
contributers.destroy()
def contributers():
contributers = Toplevel(table)
table.destroy()
def firstpage():
firstpage = Toplevel(letsbegin)
letsbegin.destroy()
def secondpage():
secondpage = Toplevel(firstpage)
firstpage.destroy()
def leave():
exit()
root = Tk()
root.title("Team Blue")
label1 = label(menu, text="Team Blue", bg = "Yellow", fg="Black")
button1 = button(menu, text="ENTER", width=15, bg="yellow", fg="Black", command =contributers)
button2 = button(menu, text="Exit", bg="red", fg="white", command=leave)
root.mainloop()
I just want this code to run
You have many mistakes which I mentioned in comments.
If you want to close one window and open new one then destroy first window - root.destroy() - and later use again Tk() to create new window and use again mainloop().
I assign new window to global variable root so I can use almost the same code to close second window and open third one.
I use global root so variable root is not local variable but it is global and I have access (to window assigned to root) in other functions.
from Tkinter import *
# --- functions ---
def open_first_window():
global root
root = Tk()
label1 = Label(root, text="Team Brake 'Em")
label1.pack()
button1 = Button(root, text="Open Second Window", command=open_second_window)
button1.pack()
button2 = Button(root, text="Exit", command=root.destroy)
button2.pack()
root.mainloop()
def open_second_window():
global root
root.destroy()
root = Tk()
label1 = Label(root, text="Second Window")
label1.pack()
button1 = Button(root, text="Open Third Window", command=open_third_window)
button1.pack()
button2 = Button(root, text="Exit", command=root.destroy)
button2.pack()
root.mainloop()
def open_third_window():
global root
root.destroy()
root = Tk()
label1 = Label(root, text="Third Window")
label1.pack()
button2 = Button(root, text="Exit", command=root.destroy)
button2.pack()
root.mainloop()
# --- main ---
open_first_window()
There is other popular method - don't destry window but remove all widgets and put new one. Widget Frame can be usful because you can put all widget in Frame and Frame put in Window and later you have to only remove Frame and put new Frame with new widgets.
from Tkinter import *
# --- function ---
def create_first_frame():
global root
global frame
#frame.destroy()
frame = Frame()
frame.pack()
label1 = Label(frame, text="Team Brake 'Em")
label1.pack()
button1 = Button(frame, text="Open Second Window", command=create_second_frame)
button1.pack()
button2 = Button(frame, text="Exit", command=root.destroy)
button2.pack()
def create_second_frame():
global root
global frame
frame.destroy()
frame = Frame()
frame.pack()
label1 = Label(frame, text="Second Window")
label1.pack()
button1 = Button(frame, text="Open Third Window", command=create_third_frame)
button1.pack()
button2 = Button(frame, text="Exit", command=root.destroy)
button2.pack()
def create_third_frame():
global root
global frame
frame.destroy()
frame = Frame()
frame.pack()
label1 = Label(frame, text="Third Window")
label1.pack()
button2 = Button(frame, text="Exit", command=root.destroy)
button2.pack()
# --- main ---
root = Tk()
create_first_frame()
root.mainloop()
this is because as you wrapped your whole code inside the fuction name team().
so, you have to call that method at appropriate position in order to run the program.
and please make sure the letter case as label fuction is written as Label() so does button() is Button().
and also you have to use root in place of menu, then hopefully you see window.
pack the content according.
I am trying to open a popup window on a click. The same is not getting opened as a popup, but another tab in the already opened window. Also, when I maximize the window the click function doesn't work at all.
My code is as under:
from tkinter import *
from tkinter import ttk
import shelve
import pyperclip
import os
def popupmsg(msg):
popup = Tk()
popup.wm_title("!")
label = ttk.Label(popup, text=msg)
label.pack(side="top", fill="x", pady=10)
B1 = ttk.Button(popup, text="Okay", command = popup.destroy)
B1.pack()
popup.mainloop()
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.init_window()
def init_window(self):
self.master.title("uClip - Your personal clipboard")
self.pack(fill = BOTH, expand =1)
global paste_entry
paste_entry = Entry(self)
paste_entry.grid(row=1, sticky="e")
global copy_entry
copy_entry = Entry(self)
copy_entry.grid(row=2, sticky="e")
global delete_entry
delete_entry = Entry(self)
delete_entry.grid(row=3, sticky="e")
button1 = Button(self, text="Paste to uClip", command= lambda:popupmsg("Pop Up")).grid(row=1,column=1)
button2 = Button(self, text="Copy from uClip", command=None).grid(row=2,column=1)
button4 = Button(self, text="Delete from uClip",command=None).grid(row=3, column=1)
button3 = Button(self, text="List all Keywords",command=None).grid(row=4, sticky="e")
button5 = Button(self, text="Clear uClip",command=None).grid(row=5, sticky="e")
root = Tk()
root.geometry("400x300")
root.resizable(None, None)
uClip = Window(root)
root.mainloop()
I have written a code in python 2.7 and i have created a root window with two buttons: submit and cancel when i press submit button, a new window opens and previous parent/root window gets iconify. Now i want that when i cut the child window, parent window should be deiconify, i am in a trouble that where to put the condition so that parent window gets deiconify?
from Tkinter import *
root = Tk()
root.title("test window")
root.geometry('400x220+500+250')
root.configure(background="dark gray")
def submit(*args):
root.iconify()
popup_root_window = Toplevel()
popup_root_window.geometry('300x50+550+300')
popup_root_window.resizable(width=False, height=False)
popup_root_window.focus_force()
popup_root_window.grab_set()
popup_root_window_label = Label(popup_root_window, text="new window open successfully.")
popup_root_window_label.pack(anchor=CENTER, padx=10, pady=20)
frame = Frame(root, bd=4, relief="raise", height=100, width=250)
frame.pack(fill="both", padx=70, pady=35)
frame.pack_propagate(0)
submit_button = Button(frame, text="Submit", command=submit, width=10)
submit_button.grid(row=0, column=0)
cancel_button = Button(frame, text="Cancel", width=10)
cancel_button.grid(row=0, column=1)
root.mainloop()
I wrote the below in 3.6 but it should still work in 2.7.
from tkinter import *
class App:
def __init__(self, root):
self.root = root
self.label1 = Label(self.root, text="I'm your main window.")
self.button = Button(self.root, text="Submit", command=self.command)
self.label1.pack()
self.button.pack()
def command(self):
self.root.iconify()
self.top = Toplevel(self.root)
self.label2 = Label(self.top, text="I'm your toplevel window.")
self.label2.pack()
self.top.protocol("WM_DELETE_WINDOW", self.close)
def close(self):
self.top.destroy()
self.root.deiconify()
root = Tk()
App(root)
root.mainloop()
You can use .protocol() on the event WM_DELETE_WINDOW to create a callback whenever the X is selected from the title bar of the designated window.
This is tested and working with Python 3.6 on Windows 10.
You can try this one. i have tested this in python 2.7.13 and it working correctly.
from Tkinter import *
root = Tk()
root.title("test window")
root.geometry('400x220+500+250')
root.configure(background="dark gray")
def submit(*args):
def on_closing(*args):
popup_root_window.destroy()
root.deiconify()
root.iconify()
popup_root_window = Toplevel()
popup_root_window.geometry('300x50+550+300')
popup_root_window.resizable(width=False, height=False)
popup_root_window.focus_force()
popup_root_window.grab_set()
popup_root_window_label = Label(popup_root_window, text="new window open successfully.")
popup_root_window_label.pack(anchor=CENTER, padx=10, pady=20)
popup_root_window.protocol("WM_DELETE_WINDOW", on_closing)
frame = Frame(root, bd=4, relief="raise", height=100, width=250)
frame.pack(fill="both", padx=70, pady=35)
frame.pack_propagate(0)
submit_button = Button(frame, text="Submit", command=submit, width=10)
submit_button.grid(row=0, column=0)
cancel_button = Button(frame, text="Cancel", width=10)
cancel_button.grid(row=0, column=1)
root.mainloop()