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()
Related
I have created a program in Python using tkinter. I have created two seperate classes for two windows. I am opening the other window by clicking on the button of one window. I want it such that when new window opens the other should close. My code is
from tkinter import Tk, Toplevel
from tkinter import *
def main():
main_window = Tk()
app = first(main_window)
main_window.mainloop()
class first:
def __init__(self, root):
self.root = root
self.root.title('First window')
self.root.geometry('1350x700+0+0')
frame1 = Frame(self.root, bg='black')
frame1.place(x=400, y=50, width=400, height=600)
btn_1 = Button(frame1, command=self.second_window, text='open second window', font=("Times New Roman", 15, 'bold'), bd=3,
relief=RIDGE,
cursor='hand2', bg='red', fg='white', activeforeground='white', activebackground='red')
btn_1.place(x=100, y=350, width=220, height=35)
def second_window(self):
self.new_window = Toplevel(self.root)
self.app = second(self.new_window)
class second:
def __init__(self, root):
self.root = root
self.root.title('Second Window')
self.root.geometry("1350x700+0+0")
self.root.config(bg='white')
frame1 = Frame(self.root, bg='black')
frame1.place(x=400, y=50, width=400, height=600)
btn_1 = Button(frame1, command=self.first_window, text='open first window',
font=("Times New Roman", 15, 'bold'), bd=3,
relief=RIDGE,
cursor='hand2', bg='red', fg='white', activeforeground='white', activebackground='red')
btn_1.place(x=100, y=350, width=220, height=35)
def first_window(self):
self.new_window = Toplevel(self.root)
self.app = first(self.new_window)
if __name__ == '__main__':
main()
I understand this question is quite common but I cant find a soloution on here which would be applicable for my code.
You can destroy the previous window and start a new window using Tk class
Here is an example
from tkinter import *
from tkinter.ttk import Button
root = Tk()
root.title("title")
root.geometry("800x500")
def window2():
root.destroy()
window2_main = Tk()
Label(window2_main, text="Bye Bye").pack()
window2_main.mainloop()
a = Button(text="Click This", command=window2)
a.pack()
root.mainloop()
the buttons should be packed in the LabelFrame but they don't
they are packed in the root screen
the Code:
import tkinter as tk
root = tk.Tk()
root.geometry('300x300')
frame = tk.LabelFrame(root, text='Hello')
frame.pack(expand='yes', fill='both', padx=10, pady=10)
btn1 = tk.Button(frame, text='Button 1').pack()
btn2 = tk.Button(frame, text='Button 2').pack()
root.mainloop()
Although this is not causing the problem you have mentioned, just be careful that .pack() should not be used inline when you are assigning the btn1, btn2 variables to Button object. Here is the modified code -
import tkinter as tk
root = tk.Tk()
root.geometry('300x300')
frame = tk.LabelFrame(root, text='Hello')
frame.pack(expand='yes', fill='both', padx=10, pady=10)
btn1 = tk.Button(frame, text='Button 1')
btn1.pack()
btn2 = tk.Button(frame, text='Button 2')
btn2.pack()
root.mainloop()
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 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()
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()