I'm new to python and I have to program some sort of gui. But the gui consist of child windows, currently there is one child window. The prolem is at startup the child window also starts up. It's supposed to wait until the button has been clicked and then launch the child window. I have no idea why it beheaves like this....
#!/usr/bin/env python
from Tkinter import *
import tkMessageBox as box
import rospy
class gui(Frame):
def __init__(self, parent):
Frame.__init__(self, parent, background="white")
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Baxter analyse tool")
menubar = Menu(self.parent)
self.parent.config(menu=menubar)
fileMenu = Menu(menubar)
submenu = Menu(fileMenu)
submenu.add_command(label="camera tool", command=self.camera_window())
submenu.add_command(label="range tool")
submenu.add_command(label="control tool")
submenu.add_command(label="sonar tool")
submenu.add_command(label="quick check tool")
fileMenu.add_cascade(label="tools", menu=submenu, underline=0)
fileMenu.add_separator()
fileMenu.add_command(label="Exit", command=self.onExit)
menubar.add_cascade(label="File", menu=fileMenu)
menubar.add_command(label="about", command=self.about)
def camera_window(self):
cameraGui = CameraGui()
def about(self):
box.showinfo("Baxter","Analyse tool.")
def onExit(self):
self.quit()
class CameraGui(object):
def __init__(self):
self.initUI()
def initUI(self):
win = Toplevel()
Label(win, text="testestest").pack()
Button(win, text="hello", command=win.destroy).pack()
def main():
rospy.init_node('baxter_lput_analyse_tool')
root = Tk()
root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth()/2, root.winfo_screenheight()-50))
root.focus_set()
root.bind("<Escape>", lambda e: e.widget.quit())
app = gui(root)
root.mainloop()
if __name__=='__main__':
main()
The program runs fine just that it automatically opens the child window
Don't call the function self.camera_window().
Remove the (). Your self.camera_window method gets called as soon as the main loop starts.
Do this:
submenu.add_command(label="camera tool", command=self.camera_window)
Or if you want to send some argument then:
submenu.add_command(label="camera tool", command=lambda:self.camera_window(args))
Related
I am creating an tkinter app. For now I just want to get a very basic menubar to work, with a file section, and an exit button in the sub menu. Here is my object oriented code, which may be where I am going wrong:
import tkinter as tk
class MainApplication(tk.Frame):
def __init_(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.menubar = tk.Menu(self)
self.filemenu = tk.Menu(self.menubar, tearoff=0)
self.filemenu.add_command(label="Exit", command=self.quit)
self.menubar.add_cascade(label="File", menu=self.filemenu)
self.config(menu=self.menubar)
if __name__ == "__main__":
root = tk.Tk()
MainApplication(root).pack(side="top", fill="both", expand=True)
root.mainloop()
However, this only creates a blank tkinter window. This usually works for me when I use procedural programming so I think I am doing something wrong with OOP. I am trying to say self.config() as root.config(), but this does not work.
2 big issues there. The first is that you misspelled __init__, so none of your custom code is being run. The second is that you need to apply the menu to the root window, aka self.master (default name) or self.parent (your name). Try like this:
import tkinter as tk
class MainApplication(tk.Frame):
def __init__(self, master=None, **kwargs):
super().__init__(master, **kwargs)
self.menubar = tk.Menu(self)
self.filemenu = tk.Menu(self.menubar, tearoff=0)
self.filemenu.add_command(label="Exit", command=self.quit)
self.menubar.add_cascade(label="File", menu=self.filemenu)
self.master.config(menu=self.menubar)
if __name__ == "__main__":
root = tk.Tk()
root.geometry('200x200') # remove once you've added window content
win = MainApplication(root)
win.pack(side="top", fill="both", expand=True)
root.mainloop()
I also moved you to a python3 inheritance style, and defined a size so that you actually see something.
import tkinter as tk
class Main:
def __init__(self, parent):
self.parent = parent
self.button = tk.Button(text="Build", command=self.new_window)
self.button.grid(row=1, column=0)
def new_window(self):
self.window = tk.Tk()
self.app = Graph(self.window)
self.window.mainloop()
class Graph:
def __init__(self, parent):
self.parent = parent
self.new_button = tk.Button(text="text")
self.new_button.grid(in_=self.parent)
def main():
root = tk.Tk()
app = Main(root)
root.mainloop()
if __name__ == "__main__":
main()
Here's my code and I'm trying to create a button on a new widget using grid() + in_, but there is a problem - button dont creates on a new widget instead of this it creates on the main one.
In tkinter widgets are by default assigned to root window, Tk unless passed a parent widget as the first positional argument, which is omitted in:
self.new_button = tk.Button(text="Destroy", command=self.destroy)
You should replace it with:
self.new_button = tk.Button(parent, text="Destroy", command=self.destroy)
Sorry for any stupidity or ignorance on my part but I am new to Python, and coding in general. I have been trying to get a UI working for a game I'm going to make as a way of teaching myself python I have a main window and a few button widgets in this.
The Play game button opens a second window (for the game itself) and hides the root window with the .withdraw method. This works perfectly. Inside the game window I have another button which I would like to destroy the game window and bring the user back to the root menu window that I have withdrawn. This seems to work except every time I call it,it creates an new, duplicate set of widgets in the window so I end up with multiple sets.
I'll post my full code at the bottom but here are what I believe are the relevant parts.
A tkinter Button calls this inside the parent class (the main window). This works fine.
def playGame(self): #Start the main game window
self.master.withdraw()
gameWindow()
I'm using the bellow method inside of the child class to destroy the game window and then call a method in the parent class to bring back the earlier withdrawn window
def exitMenu(self):
self.g.destroy()
UI(root).showMenu()
this works except it duplicates the widgets each time resulting in this being shown:
screen capture of result
Bellow is all my code, thank you so much for any help.
import tkinter as tk
import PIL
from Config import *
root = tk.Tk()
class UI(): #Main Menu
def __init__(self, master):
#Create Main Menu Window
self.master = master
self.master.title("Monopoly")
self.master.wm_iconbitmap('icons\Monopoly-Icon.ico')
self.master.geometry((resolution))
#Menu Buttons
self.label = tk.Label(master, text= 'Welcome to Monopoly! PLACEHOLDER')
self.playButton = tk.Button(master, text= 'Play Game', command= self.playGame)
self.settingsButton = tk.Button(master, text= 'settings', command= self.settings)
self.exitButton = tk.Button(master, text= 'Exit', command= self.exitGame)
self.label.grid(columnspan=2)
self.playButton.grid(column=0)
self.settingsButton.grid(column=0)
self.exitButton.grid(column=0)
def settings(self): #Opens Settings Window
s = tk.Toplevel()
s.title('Settings')
s.wm_iconbitmap('icons\Monopoly-Icon.ico')
s.geometry((resolution))
self.master.withdraw()
resLabel = tk.Label(s, text= 'Resolution')
resOption = tk.OptionMenu(s, resolution, *resList)
resLabel.grid(column=0,row=0)
resOption.grid(column=0, row=4)
def showMenu(self): #Bring back menu windwow
self.master.deiconify()
def exitGame(self): #Exit Game Method
root.destroy()
def playGame(self): #Start the main game window
self.master.withdraw()
gameWindow()
class gameWindow(UI):
def __init__(self):
self.g = tk.Toplevel()
self.g.title('Monopoly')
self.g.wm_iconbitmap('icons\Monopoly-Icon.ico')
self.g.geometry((resolution))
self.menuButton = tk.Button(self.g, text= 'Main Menu', command= self.exitMenu)
self.menuButton.grid(column=0,row=0)
def exitMenu(self):
self.g.destroy()
UI(root).showMenu()
mainMenu = UI(root)
root.mainloop()
You should pass the parent class, UI, to the child class gameWindow, then you can use the showMenu() method of the parent class in the child class.
Code for showing the main window from the child window will be:
self.mainUI.showMenu() instead of UI(root).showMenu()
Child class becomes:
class gameWindow(UI):
def __init__(self, mainUI): # Make the main window a parameter/attribute
self.mainUI = mainUI # Make the main window a parameter/attribute
self.g = tk.Toplevel()
self.g.title('Monopoly')
self.g.wm_iconbitmap('icons\Monopoly-Icon.ico')
self.g.geometry((resolution))
self.menuButton = tk.Button(self.g, text= 'Main Menu', command= self.exitMenu)
self.menuButton.grid(column=0,row=0)
def exitMenu(self):
self.g.destroy()
self.mainUI.showMenu()
gameWindow now requires one argument, so you pass it the main UI as self
def playGame(self): #Start the main game window
self.master.withdraw()
gameWindow(self)
You can read more about hiding/showing window here
Here is the full code:
import tkinter as tk
import PIL
from Config import *
root = tk.Tk()
class UI(): #Main Menu
def __init__(self, master):
#Create Main Menu Window
self.master = master
self.master.title("Monopoly")
self.master.wm_iconbitmap('icons\Monopoly-Icon.ico')
self.master.geometry((resolution))
#Menu Buttons
self.label = tk.Label(master, text= 'Welcome to Monopoly! PLACEHOLDER')
self.playButton = tk.Button(master, text= 'Play Game', command= self.playGame)
self.settingsButton = tk.Button(master, text= 'settings', command= self.settings)
self.exitButton = tk.Button(master, text= 'Exit', command= self.exitGame)
self.label.grid(columnspan=2)
self.playButton.grid(column=0)
self.settingsButton.grid(column=0)
self.exitButton.grid(column=0)
def settings(self): #Opens Settings Window
s = tk.Toplevel()
s.title('Settings')
s.wm_iconbitmap('icons\Monopoly-Icon.ico')
s.geometry((resolution))
self.master.withdraw()
resLabel = tk.Label(s, text= 'Resolution')
resOption = tk.OptionMenu(s, resolution, *resList)
resLabel.grid(column=0,row=0)
resOption.grid(column=0, row=4)
def showMenu(self): #Bring back menu windwow
self.master.deiconify()
def exitGame(self): #Exit Game Method
root.destroy()
def playGame(self): #Start the main game window
self.master.withdraw()
gameWindow(self)
class gameWindow(UI):
def __init__(self, mainUI): # Make the main window a parameter/attribute
self.mainUI = mainUI # Make the main window a parameter/attribute
self.g = tk.Toplevel()
self.g.title('Monopoly')
self.g.wm_iconbitmap('icons\Monopoly-Icon.ico')
self.g.geometry((resolution))
self.menuButton = tk.Button(self.g, text= 'Main Menu', command= self.exitMenu)
self.menuButton.grid(column=0,row=0)
def exitMenu(self):
self.g.destroy()
self.mainUI.showMenu()
mainMenu = UI(root)
root.mainloop()
I am creating 2 window in my program and i am using two class, since the code is complex, i separate it in 2 different python file. After i imported the second window file, how can i make sure it open without having this error which show in this picture
The original result should look like this after the new window button clicked:
Coding for Main Window:
from tkinter import *
import classGUIProgram
class Window(Tk):
def __init__(self, parent):
Tk.__init__(self, parent)
self.parent = parent
self.initialize()
def initialize(self):
self.geometry("600x400+30+30")
self.wButton = Button(self, text='newWindow', command = self.OnButtonClick)
self.wButton.pack()
def OnButtonClick(classGUIProgram):
classGUIProgram.top = Toplevel()
master = Tk()
b = classGUIProgram.HappyButton(master)
master.mainloop()
if __name__ == "__main__":
window = Window(None)
window.title("title")
window.mainloop()
Coding for Second Window:
from tkinter import *
class HappyButton:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.printButton = Button(frame, text="Print message", command=self.printMessage)
self.printButton.pack(side=LEFT)
self.quitButton = Button(frame, text="Quit", command= quit)
self.quitButton.pack(side=LEFT)
self.downloadHistoryCB=Checkbutton(frame, text="Download History")
self.downloadHistoryCB.pack(side=LEFT)
def printMessage(self):
print("Wow this actually worked!")
master = Tk()
b = HappyButton(master)
master.mainloop()
You're creating extra Tk windows. Here is an example of using Toplevel widgets and another file.
mainWindow.py
import tkinter as tk
import secondWindow as sW
class MainWindow(tk.Tk):
def __init__(self):
super().__init__()
self.title("Main Window")
self.geometry("600x400+30+30")
tk.Button(self, text = "New Window", command = self.new_window).pack()
tk.Button(self, text = "Close Window", command = self.close).pack()
self._second_window = None
def new_window(self):
# This prevents multiple clicks opening multiple windows
if self._second_window is not None:
return
self._second_window = sW.SubWindow(self)
def close(self):
# Destory the 2nd window and reset the value to None
if self._second_window is not None:
self._second_window.destroy()
self._second_window = None
if __name__ == '__main__':
window = MainWindow()
window.mainloop()
secondWindow.py
import tkinter as tk
class SubWindow(tk.Toplevel):
def __init__(self, master):
super().__init__(master)
self.title("Sub Window")
self.geometry("400x300+30+30")
# Change what happens when you click the X button
# This is done so changes also reflect in the main window class
self.protocol('WM_DELETE_WINDOW', master.close)
tk.Button(self, text = "Print", command = self.printMessage).pack()
def printMessage(self):
print("Wow this actually worked!")
When using another file be sure to not have any global code you don't want running. Your classes don't have to inherit from Tk and Toplevel, this is just an example. But you need to ensure you only ever have one instance of Tk otherwise you get the behaviour you encountered
I'm trying to add a window icon to the child windows of a main window, built in TKinter. For portability (aka no missing files) I've got the main window icon set from a base64 encoded gif as a variable. I've been unable to replicate this for the child windows.
from Tkinter import *
ICON = """
R0lGODlhMgAyAPcAAAAAAAAAMwAAZgAAmQAAzAAA/wArAAArMwArZgArmQArzAAr/wBVAABVMwBV
ZgBVmQBVzABV/wCAAACAMwCAZgCAmQCAzACA/wCqAACqMwCqZgCqmQCqzACq/wDVAADVMwDVZgDV
mQDVzADV/wD/AAD/MwD/ZgD/mQD/zAD//zMAADMAMzMAZjMAmTMAzDMA/zMrADMrMzMrZjMrmTMr
zDMr/zNVADNVMzNVZjNVmTNVzDNV/zOAADOAMzOAZjOAmTOAzDOA/zOqADOqMzOqZjOqmTOqzDOq
/zPVADPVMzPVZjPVmTPVzDPV/zP/ADP/MzP/ZjP/mTP/zDP//2YAAGYAM2YAZmYAmWYAzGYA/2Yr
AGYrM2YrZmYrmWYrzGYr/2ZVAGZVM2ZVZmZVmWZVzGZV/2aAAGaAM2aAZmaAmWaAzGaA/2aqAGaq
M2aqZmaqmWaqzGaq/2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yKYdWWcfNpptpKXmFYWxSZYXijhXWNNGM9NXWoVJBxjgXTCsWOSSS0CXpEk6X2eicb1DC
hduVVSIEY5MOZhnWli95KaaXAQEAOw==
"""
class ChildWindow(Toplevel):
def __init__(self, callingFrame):
self.parent = callingFrame
Toplevel.__init__(self)
self.title("Child")
self.initUI()
def initUI(self):
#this doesn't work in this class, unlike the first class where it does work.
global ICON
self.icon = PhotoImage(data=ICON)
self.call('wm', 'iconphoto', self.parent._w, self.icon)
btn = Button(self, text="Close", command=self.destroy)
btn.pack()
class ParentWindow(object):
def __init__(self, parent):
self.parent = parent
self.parent.title("Parent")
self.frame = Frame(parent)
self.initUI()
def initUI(self):
global ICON
self.icon = PhotoImage(data=ICON)
self.parent.call('wm', 'iconphoto', self.parent._w, self.icon)
btn = Button(self.parent, text="Child", command=self.opennew)
btn.pack()
def opennew(self):
newwin = ChildWindow(self)
def main():
root = Tk()
win = ParentWindow(root)
root.geometry()
root.mainloop()
if __name__ == '__main__':
main()
Replace:
self.call('wm', 'iconphoto', self.parent._w, self.icon)
with:
self.tk.call('wm', 'iconphoto', self._w, self.icon)
I thought I'd share how to do a similar thing without using classes. In my example, I used parent and child to refer to the windows and used different background colors on the windows to make it obvious what happens where and I added comments to the sections that pertain to the icons.
from tkinter import *
parent = Tk()
parent.title("PARENT")
parent.geometry("200x200")
parent.config(bg="yellow")
# Define the icon:
ICON = """
R0lGODlhMgAyAPcAAAAAAAAAMwAAZgAAmQAAzAAA/wArAAArMwArZgArmQArzAAr/wBVAABVMwBV
ZgBVmQBVzABV/wCAAACAMwCAZgCAmQCAzACA/wCqAACqMwCqZgCqmQCqzACq/wDVAADVMwDVZgDV
mQDVzADV/wD/AAD/MwD/ZgD/mQD/zAD//zMAADMAMzMAZjMAmTMAzDMA/zMrADMrMzMrZjMrmTMr
zDMr/zNVADNVMzNVZjNVmTNVzDNV/zOAADOAMzOAZjOAmTOAzDOA/zOqADOqMzOqZjOqmTOqzDOq
/zPVADPVMzPVZjPVmT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C
rYFjFKhVeVfIuV+b3shXeXN71Zv+zv3X9fS/05MyoIbX2fvQ8mSZQyedXTn63LedbjaOvTPj7Y/r
g8e9+XrH+JhdNN9XA96nXnQP9dfTfP6BlRN9Ag6o2GLIDXWZeQlOOOFwPA0GIXsaIvahQebNFOJ0
j91VImsyKYdWWcfNpptpKXmFYWxSZYXijhXWNNGM9NXWoVJBxjgXTCsWOSSS0CXpEk6X2eicb1DC
hduVVSIEY5MOZhnWli95KaaXAQEAOw==
"""
# Choose the icon:
icon = PhotoImage(data=ICON)
# Make a parent call for the icon from within the parent:
parent.call("wm", "iconphoto", parent._w, icon)
def new_child():
child = Toplevel(parent)
child.title("CHILD")
child.geometry("200x200")
child.config(bg="green")
# Make a parent call for the icon from within the child:
parent.call("wm", "iconphoto", child._w, icon)
childbutton = Button(child, text="Close Child", command=child.destroy)
childbutton.pack(pady=10)
parentbutton1 = Button(parent, text="Open Child", command=new_child)
parentbutton1.pack(pady=10)
parentbutton2 = Button(parent, text="Close Parent", command=parent.destroy)
parentbutton2.pack(pady=5)
parent.mainloop()