So for some reason the scale function in tkinter doesn't want to output the number on the scale. All i receive is either 0.0 or nothing. It seems to be to do with the GUI and calling functions through the GUI. Written and run in python 3.4.
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
iterations=30
def settings():
global itervar, iterscale
sGui = Tk()
itervar = DoubleVar()
iterscale = Scale(sGui, orient="horizontal", from_=1, to=1000, variable=itervar)
iterscale.pack()
iterbutton = Button(sGui, text="Save Settings", command=saveSettings)
iterbutton.pack()
sGui.mainloop()
def saveSettings():
global iterations
iterations = itervar.get()
print(iterations)
def doNothing():
pass
def main():
global root, version
root= Tk()
menu = Menu(root)
root.config(menu=menu)
fileMenu = Menu(menu)
menu.add_cascade(label="File", menu=fileMenu)
fileMenu.add_command(label="Quit", command=quit)
benchmarkMenu = Menu(menu)
menu.add_cascade(label="Benchmark", menu=benchmarkMenu)
benchmarkMenu.add_command(label="Run [All]", command=doNothing)
benchmarkMenu.add_separator()
benchmarkMenu.add_command(label="Settings", command=settings)
root.mainloop()
#Main
main()
I have tried the functions settings and saveSettings on their own and they work fine, but when i call it through the GUI it doesn't seem to work.
Any ideas on the problem, my only solution would be have the settings function and saveSettings function in a different file and then run that file externally with os.startfile("etc...")
Minimal fix: change this
itervar = DoubleVar()
to this:
itervar = DoubleVar(sGui)
Because you have two root applications (root and sGui are both instances of Tk) the implied parent widget for itervar is the first one created, being root so tkinter gets confused when you specify it as the variable for a completely different application.
But I would highly recommend using a Toplevel instance to keep the windows a part of the same program:
sGui = Toplevel(root)
...
#sGui.mainloop() #no longer need this
although if you want to be able to run the setting window without the main one you might consider making all your visible windows Toplevels and make the actual root hidden:
# I'm not sure if you want to call it this
abs_root = Tk() # but you are already using root
abs_root.withdraw() #hide the window
Then make root = Toplevel(abs_root)
You coud phase out the variable all together by using .geting the scale directly:
iterations = iterscale.get()
Related
I have been creating an application for taking the test. So, for that, I have to do two things.
First, disable the drag of the Tkinter window and don't let the user focus on other windows rather than my application window. This means I wanted to make my application such that, No other application can be used while my application is in use.
Try this:
import tkinter as tk
class FocusedWindow(tk.Tk):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# Force it to be unminimisable
super().overrideredirect(True)
# Force it to always be on the top
super().attributes("-topmost", True)
# Even if the user unfoceses it, focus it
super().bind("<FocusOut>", lambda event: self.focus_force())
# Take over the whole screen
width = super().winfo_screenwidth()
height = super().winfo_screenheight()
super().geometry("%ix%i+0+0" % (width, height))
root = FocusedWindow()
# You can use it as if it is a normal `tk.Tk()`
button = tk.Button(root, text="Exit", command=root.destroy)
button.pack()
root.mainloop()
That removed the title bar but you can always create your own one by using tkinter.Labels and tkinter.Buttons. I tried making it work with the title bar but I can't refocus the window for some reason.
One way to do this is by the following, another could be to overwrite the .geometry() method of tkinter.
In the following code I simply had get the position by using winfo_rootx and winfo_rooty. After this you can force the window by calling the geometry method via binding the event every time the window is configured.
import tkinter as tk
def get_pos():
global x,y
x = root.winfo_rootx()
y = root.winfo_rooty()
def fix_pos():
root.bind('<Configure>', stay_at)
def stay_at(event):
root.geometry('+%s+%s' % (x,y))
root = tk.Tk()
button1 = tk.Button(root, text='get_pos', command=get_pos)
button2 = tk.Button(root, text='fix_pos', command=fix_pos)
button1.pack()
button2.pack()
root.mainloop()
I'm new to python programming, and I have a bit of trouble with the program structure:
When I make my GUI in the main python part then the code works:
import tkinter as tk
root = tk.Tk()
root.overrideredirect(True)
root.geometry("800x480")
def cb_Gebruiker():
btnUser["text"]= "changed"
btnUser = tk.Button(root, text="User",command = cb_Gebruiker)
btnUser.place(x=1,y=1,width="300",height="73")
root.mainloop()
When I make my GUI in a function, the btn variable is local, so this doesn't work
def MakeBtn():
btnUser = tk.Button(root, text="User",command = cb_Gebruiker)
btnUser.place(x=1,y=1,width="300",height="73")
def cb_Gebruiker():
btnUser["text"]= "changed"
MakeBtn()
root.mainloop()
Now I have a program that's rather large and I want my GUI in a separate file, but then I can't access my GUI components...
And I can't seem to be able to find a tutorial on how to structure a program (python has to many possibilities: script, module, object oriented,..)
How do I solve this?
You will need a lambda to delay the call to the command with a parameter.
def MakeBtn():
btnUser = tk.Button(root, text="User", command = lambda: cb_Gebruiker(btnUser))
btnUser.place(x=1, y=1, width="300", height="73")
def cb_Gebruiker(btnUser):
btnUser["text"] = "changed"
MakeBtn()
root.mainloop()
I created a couple of GUIs using tkinter. But now I am interested in combining them into one caller GUI. So the caller GUI would have buttons that, when clicked, would open the other GUIs. However, I cannot get it to work. I've done the imports correctly (I think), edited the main functions in the subGUIs, and added the command=GUI.main in my buttons. I get it to load but I get errors about missing files...but when I run a GUI by itself it works fine.
In my research, I read that there can only be one mainloop in a Tkinter program. Basically, I cannot use a Tkinter GUI to call another Tkinter GUI. Do you know what I can do different, for instance, can I create the caller GUI using wxPython and have it call all other GUIs that use Tkinter?
Thank you!
You can't "call" another GUI. If this other GUI creates its own root window and calls mainloop(), your only reasonable option is to spawn a new process. That's a simple solution that requires little work. The two GUIs will be completely independent of each other.
If you have control over the code in both GUIs and you want them to work together, you can make the base class of your GUI a frame rather than a root window, and then you can create as many windows as you want with as many GUIs as you want.
For example, let's start with a simple GUI. Copy the following and put it in a file named GUI1.py:
import tkinter as tk
class GUI(tk.Frame):
def __init__(self, window):
tk.Frame.__init__(self)
label = tk.Label(self, text="Hello from %s" % __file__)
label.pack(padx=20, pady=20)
if __name__ == "__main__":
root = tk.Tk()
gui = GUI(root)
gui.pack(fill="both", expand=True)
tk.mainloop()
You can run that GUI normally with something like python GUI1.py.
Now, make an exact copy of that file and name it GUI2.py. You can also run it in the same manner: python GUI2.py
If you want to make a single program that has both, you can create a third file that looks like this:
import tkinter as tk
import GUI1
import GUI2
# the first gui owns the root window
win1 = tk.Tk()
gui1 = GUI1.GUI(win1)
gui1.pack(fill="both", expand=True)
# the second GUI is in a Toplevel
win2 = tk.Toplevel(win1)
gui2 = GUI2.GUI(win2)
gui2.pack(fill="both", expand=True)
tk.mainloop()
Depending on your OS and window manager, one window might be right on top of the other, so you might need to move it to see both.
Thank you for the ideas. At first, your code wouldn't print the text on the toplevel window. So I edited it a little and it worked! Thank you. GUI1 and GUI2 look like:
import tkinter as tk
def GUI1(Frame):
label = tk.Label(Frame, text="Hello from %s" % __file__)
label.pack(padx=20, pady=20)
return
if __name__ == "__main__":
root = tk.Tk()
GUI1(root)
root.mainloop()
And then the caller looks like this:
from tkinter import *
import GUI1
import GUI2
def call_GUI1():
win1 = Toplevel(root)
GUI1.GUI1(win1)
return
def call_GUI2():
win2 = Toplevel(root)
GUI2.GUI2(win2)
return
# the first gui owns the root window
if __name__ == "__main__":
root = Tk()
root.title('Caller GUI')
root.minsize(720, 600)
button_1 = Button(root, text='Call GUI1', width='20', height='20', command=call_GUI1)
button_1.pack()
button_2 = Button(root, text='Call GUI2', width='20', height='20', command=call_GUI2)
button_2.pack()
root.mainloop()
How in TTK can I use the OS default loader theme instead of the tkinter one?
This is an example of a normal progress bar.
from tkinter import *
from tkinter.ttk import *
root = Tk()
root.geometry('400x250+1000+300')
progress_bar = Progressbar(root, orient=HORIZONTAL, length=100, mode='indeterminate')
progress_bar.pack(expand=True)
progress_bar.start()
root.mainloop()
I get a progress bar, but it isn't themed like the OS default progress bar is.
I thought that perhaps a style could make it look like that, so I listed the styles:
Style().theme_names() # ('winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative')
I tried each one like so:
from tkinter import *
from tkinter.ttk import *
root = Tk()
root.geometry('400x250+1000+300')
os_default_style = Style()
os_default_style.theme_use('THEME')
os_default_style.configure('osdefault.Horizontal.TProgressbar')
progress_bar = Progressbar(root, orient=HORIZONTAL, length=100, mode='indeterminate', style='osdefault.Horizontal.TProgressbar')
progress_bar.pack(expand=True)
progress_bar.start()
root.mainloop()
They produce ones that are similar, but none are the system. How do I make it look exactly like one used by the system?
I think is a little bit case sensitive the "default OS theme"... xpnative is the close to the native one and almost fool the eyes. I created an test to see the differences (note I use windows 7) and the result for default OS style reveal like xpnative is the winner. vista look same but is a little bigger that is all. Now if OS use simple theme, this two will not work, but other will do... For windows 10, classic tkinter theme is close to this OS theme, but tkinter.ttk will do same thing on windows 10... Is to sensitive this subject. Just on windows are some obstacles, I even do not mentioned Linux... Theme is not so important, if your project is making the job right and is close enough to default theme of the user OS from my view point is more than perfect.
Here is my test:
# Import
import tkinter as tk
import tkinter.ttk as ttk
# Logic
class Main:
def __init__(self, master):
self.master = master
# Create the progress bar
self.progress = ttk.Progressbar(self.master, value=0)
self.progress.pack(expand=False, fill="x", side="top", padx=5, pady=5)
self.progress.start()
# Create two layers for the theme
self.default = self.Layer("Default") # for default foundet ones
self.custom = self.Layer("Custom") # for manual input and debug purpose
# Create the entry and applay elements for the "custom layer"
self.entry = ttk.Entry(self.custom)
ttk.Button(self.custom, text="Apply...", width=15, command=lambda:self.ChangeTheme(self.entry.get().strip())
).pack(side="right")
self.entry.pack(expand=True, fill="x", side="left")
# Get a list of all default themes and create a button for each one
for theme in ttk.Style().theme_names(): self.ThemeDefault(theme)
def ThemeDefault(self, themeName:str) -> None:
ttk.Button(self.default, text=themeName, width=15, command=lambda:self.ChangeTheme(themeName)).pack(side="left")
def Layer(self, text:str) -> tk.Frame:
frame = tk.Frame(self.master)
label = tk.Label(frame, text=text, width=7, anchor="w")
frame.pack(expand=False, fill="x", side="top", padx=5, pady=5)
label.pack(side="left")
return frame
def ChangeTheme(self, themeName:str) -> None:
try: ttk.Style(self.master).theme_use(themeName)
except Exception as e: print(f">> EXCEPTION // {e}")
# Run if this file is the main opened one
if __name__ == "__main__":
root = tk.Tk()
Main(root)
root.mainloop()
This is what is look like the resoult.
Unfortunately, a 'truly native' widget is not possible in Tk. As you can see in the source, a theme simply sets a bunch of settings and it does not do anything you couldn't do yourself. However, finding a good theme will generally do a 'good enough' job (see ASI's answer for more details on this).
If you don't believe me, take a look at the ProgressbarDoLayout method (and related methods - found here) and you can see that Tk is handling the drawing itself and giving very little to the native software.
Try this -
from tkinter import *
from tkinter import ttk
root = Tk()
s = ttk.Style()
s.theme_use('winnative')
s.configure("Blue.Horizontal.TProgressbar", foreground='white',
background='blue')
P1 = ttk.Progressbar(root, style="Blue.Horizontal.TProgressbar",
orient="horizontal", length=600, mode="indeterminate",
maximum=4, value=1)
P1.pack()
root.mainloop()
I'm trying to use tk.Scale to change a tk.IntVar. I can do it on the first tk window but I can't on the second one. Why?
Look this poor sample:
import tkinter as tk
# main root
root = tk.Tk()
myvar = tk.IntVar()
def on_change(*args):
print("Value changed to {}".format(myvar.get()))
myvar.trace("w", on_change)
sc = tk.Scale(root, from_=1, to=10, orient=tk.HORIZONTAL, length=320, \
variable=myvar)
sc.grid()
# second root
root2 = tk.Tk()
myvar2 = tk.IntVar()
def on_change2(*args):
print("Value2 changed to {}".format(myvar2.get()))
myvar2.trace("w", on_change2)
sc2 = tk.Scale(root2, from_=1, to=10, orient=tk.HORIZONTAL, length=320, \
variable=myvar2)
sc2.grid()
root.mainloop()
What am I doing wrong?
I'm using python 3.3 on Mac.
You cannot have two instances of tkinter.Tk running simultaneously in the same thread. They will not share the Tcl interpreter properly and the one that was created first will always act as the main window.
To fix the problem, make root2 an instance of tkinter.Toplevel:
root2 = tk.Toplevel()
The tkinter.Toplevel window will be dependent on the tkinter.Tk one, which means it will share the Tcl interpreter started for the latter.