I want to open a MessageBox with several Checkboxes to choose some options.
The MessageBox works, but I cannot access the status of the checkboxes.
I can toggle the checkbox (access to the class works) but I am not able to get the status.
How can I get back the Checkboxes status in the main window?
How the program should work:
Main window can create ChooseBox
ChooseBox should give options to choose (here one Option for test example)
Mein window shall get status (tested here with the test button)
(working with python27 and windows - but on ubuntu it did not work neither)
#!/usr/bin/python3
# -*- coding: cp1252 -*-
from Tkinter import *
class ChooseBox(Tk):
def __init__(self):
Tk.__init__(self)
self.var = IntVar()
self.chk = Checkbutton(self, text="Option 1", variable=self.var)
self.chk.pack()
# Button to show the status of the checkbutton
button = Button(self, text='Show Stat',
command=lambda: self.Status(self.var))
button.pack()
def Status(self, var):
print var.get()
def message():
global Choose
Choose = ChooseBox()
Choose.mainloop()
def test():
global Choose
Choose.chk.toggle()
print Choose.var.get()
def main_wrapper(argv):
global Choose
root = Tk()
root.geometry("200x150+30+30")
Button_Frame=Frame(root)
Button_Frame.pack(side=BOTTOM, anchor=W, fill=X, expand=NO)
Button(Button_Frame, text='Make ChooseBox', command=message).pack(side=LEFT, anchor=W, padx=5, pady=5)
# Button to test access to the Box - here it toggles the Checkbutton and (should) prints the status
Button(Button_Frame, text='test', command=test).pack(side=LEFT, anchor=W, padx=5, pady=5)
Button(Button_Frame, text='Quit', command=root.quit).pack(side=RIGHT, anchor=E, padx=5)
root.mainloop()
if __name__ == '__main__':
main_wrapper(sys.argv)
You can not have two Tk() windows, the Tk window is unique and can only be called once. Instead replace the Tk in your choosebox with a Toplevel instead. Other than that I think it should work
Related
I want to create a simple tkinter GUI that popes up a message with YES or NO buttons but also has a dialog box.
import tkinter as tk
from tkinter import simpledialog
messagebox.askyesno("test", "Did you enter your name?")
my_var = simpledialog.askstring(title="Test",prompt="enter sentences:")
print(myvar)
root=tk.TK()
I don't want to have two popups I want to have only one with a dialog box big enough to type three sentences and has a yes or no button. Is there a way to accomplish this in python3?
Below is an example to build a custom dialog using simpledialog.Dialog:
import tkinter as tk
from tkinter.simpledialog import Dialog
class MyDialog(Dialog):
# override body() to build your input form
def body(self, master):
tk.Label(master, text="Enter sentences:", anchor="w").pack(fill="x")
self.text = tk.Text(master, width=40, height=10)
self.text.pack()
# need to return the widget to have first focus
return self.text
# override buttonbox() to create your action buttons
def buttonbox(self):
box = tk.Frame(self)
# note that self.ok() and self.cancel() are defined inside `Dialog` class
tk.Button(box, text="Yes", width=10, command=self.ok, default=tk.ACTIVE)\
.pack(side=tk.LEFT, padx=5, pady=5)
tk.Button(box, text="No", width=10, command=self.cancel)\
.pack(side=tk.LEFT, padx=5, pady=5)
box.pack()
# override apply() to return data you want
def apply(self):
self.result = self.text.get("1.0", "end-1c")
root = tk.Tk()
root.withdraw()
dlg = MyDialog(root, title="Test")
print(dlg.result)
root.destroy()
And the output:
I'm trying to program a very little app that should start with a window where you put a password, and that works. after that it opens another window and asks for credential that will go into a file (not developed yet!).
My problem is that I can't get the value from the second text widget in the second window. Can someone help me with that?
here's the code:
from tkinter import *
import tkinter as tk
root=Tk()
root.geometry("400x400")
root.title("password test")
def getNewWindow():
root2 = tk.Tk()
root2.geometry("200x200")
root2.title("infos")
textBox2=Text(root2, height=2, width=10)
textBox2.pack()
def retrieve_input2():
inputValue2=textBox2.get("1.0","end-1c")
if inputValue2 == "110604":
pass
buttonCommit=Button(root2, height=1, width=10, text="Commit",
command=lambda: retrieve_input2())
buttonCommit.pack()
def retrieve_input():
inputValue=textBox.get("1.0","end-1c")
if inputValue == "110604":
getNewWindow()
textBox=Text(root, height=2, width=10)
textBox.pack()
buttonCommit=Button(root, height=1, width=10, text="Commit",
command=lambda: retrieve_input())
#command=lambda: retrieve_input() >>> just means do this when i press the button
buttonCommit.pack()
mainloop()
I would like to have a window with a single button (with text "Click"). When I click the button a new window should open. And in that second window I would like to have 2 radiobuttons and a button (with text "Print"). Clicking that button ("Print") should print the current value of the variable my_variable, which indicates which Radiobutton is choosen.
The first code is missing the first window, but it prints out the correct values. The second code, where I added the first window (with the button "Click" opening the second window) prints always the default value of the variable my_variable. What should I change to get the current value every time a press the button "Print"?
I use tkinter with Python 3.7.
Working code:
import tkinter as tk
def function():
"""That function print the current value of my_variable, depending on which radiobutton was choosen."""
value = my_variable.get()
print(value)
window = tk.Tk()
window.title("Title")
window.geometry('200x200')
my_variable = tk.IntVar(value=0)
rbtn1 = tk.Radiobutton(window, text='one', value=1, variable=my_variable)
rbtn1.grid(row=0, column=0)
rbtn2 = tk.Radiobutton(window, text='two', value=2, variable=my_variable)
rbtn2.grid(row=1, column=0)
button = tk.Button(window, text="Print", command=function)
button.grid(row=2, column=0)
window.mainloop()
Not working code:
import tkinter as tk
def on_click():
def function():
"""That function shoud print the current value of my_variable, depending on which radiobutton was choosen. But it prints the default value instead."""
value = my_variable.get()
print(value)
window = tk.Tk()
window.title("Title")
window.geometry('200x200')
my_variable = tk.IntVar(value=0)
rbtn1 = tk.Radiobutton(window, text='one', value=1, variable=my_variable)
rbtn1.grid(row=0, column=0)
rbtn2 = tk.Radiobutton(window, text='two', value=2, variable=my_variable)
rbtn2.grid(row=1, column=0)
button = tk.Button(window, text="Print", command=function)
button.grid(row=2, column=0)
window_main = tk.Tk()
window_main.title("Title main")
window_main.geometry('400x400')
button = tk.Button(window_main, text="Click", command=lambda: on_click())
button.grid(row=0, column=0)
window_main.mainloop()
The problem is you're calling tk.Tk() twice. When you want to create another window, use tk.Toplevel() instead.
To avoid needing to do that, just change the one line indicated below:
import tkinter as tk
def on_click():
def function():
"""That function should print the current value of my_variable, depending on which radiobutton was chosen. But it prints the default value instead."""
value = my_variable.get()
print(value)
window = tk.Toplevel() ##### CHANGED.
window.title("Title")
window.geometry('200x200')
my_variable = tk.IntVar(value=0)
rbtn1 = tk.Radiobutton(window, text='one', value=1, variable=my_variable)
rbtn1.grid(row=0, column=0)
rbtn2 = tk.Radiobutton(window, text='two', value=2, variable=my_variable)
rbtn2.grid(row=1, column=0)
button = tk.Button(window, text="Print", command=function)
button.grid(row=2, column=0)
window_main = tk.Tk()
window_main.title("Title main")
window_main.geometry('400x400')
button = tk.Button(window_main, text="Click", command=lambda: on_click())
button.grid(row=0, column=0)
window_main.mainloop()
If you want to understand why you should avoid calling Tk() more than once, see the answer to Why are multiple instances of Tk discouraged?
See:
When you click a button in the main window, it calls on_click.
Inside on_click, you every time reassign the variable value: my_variable = tk.IntVar(value=0).
This is why the value of my_variable is not preserved between clicks of the button in the main window.
You need to keep my_variable outside on_click, and initialize it once, not on every click.
A nice way to do that is to make on_click accept it as a parameter: def on_click(my_variable), and command=lambda: on_click(my_variable).
A quick and pretty dirty way is to use global; it's acceptable in a throwaway script, but will quickly become ugly and hard to reason about.
I have a GUI using Tkinter, it has a main screen and then when you press a button a popup window appears, where you select a checkbutton and then a email will get sent to you.
Not matter what I do, I cannot read the value of the checkbutton as 1 or True it always = 0 or False.
This is my code:
import tkinter as tk
from tkinter import *
import time
root = tk.Tk()
root.title('Status')
CheckVar1 = IntVar()
def email():
class PopUp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
popup = tk.Toplevel(self, background='gray20')
popup.wm_title("EMAIL")
self.withdraw()
popup.tkraise(self)
topframe = Frame(popup, background='gray20')
topframe.grid(column=0, row=0)
bottomframe = Frame(popup, background='gray20')
bottomframe.grid(column=0, row=1)
self.c1 = tk.Checkbutton(topframe, text="Current", variable=CheckVar1, onvalue=1, offvalue=0, height=2, width=15, background='gray20', foreground='snow', selectcolor='gray35', activebackground='gray23', activeforeground='snow')
self.c1.pack(side="left", fill="x", anchor=NW)
label = tk.Label(bottomframe, text="Please Enter Email Address", background='gray20', foreground='snow')
label.pack(side="left", anchor=SW, fill="x", pady=10, padx=10)
self.entry = tk.Entry(bottomframe, bd=5, width=35, background='gray35', foreground='snow')
self.entry.pack(side="left", anchor=S, fill="x", pady=10, padx=10)
self.button = tk.Button(bottomframe, text="OK", command=self.on_button, background='gray20', foreground='snow')
self.button.pack(side="left", anchor=SE, padx=10, pady=10, fill="x")
def on_button(self):
address = self.entry.get()
print(address)
state = CheckVar1.get()
print (state)
time.sleep(2)
self.destroy()
app = PopUp()
app.update()
tk.Button(root,
text="EMAIL",
command=email,
background='gray15',
foreground='snow').pack(side=tk.BOTTOM, fill="both", anchor=N)
screen = tk.Canvas(root, width=400, height=475, background='gray15')
screen.pack(side = tk.BOTTOM, fill="both", expand=True)
def latest():
#Other code
root.after(300000, latest)
root.mainloop()
The popup works perfectly, and the email will print when entered but the value of checkbox is always 0.
I have tried:
CheckVar1 = tk.IntVar() - No success
self.CheckVar1 & self.CheckVar1.get() - No success
Removing self.withdraw() - No success
I only have one root.mainloop() in the script, I am using app.update() for the popup window because without this it will not open.
I have checked these existing questions for solution and none have helped:
Self.withdraw - Can't make tkinter checkbutton work normally when running as script
Self.CheckVar1 - TKInter checkbox variable is always 0
Only one instance of mainloop() - Python tkinter checkbutton value always equal to 0
I have also checked very similar questions but I wasn't going to post them all.
Any help is appreciated.
The problem is that you have two root windows. Each root window gets its own internal tcl interpreter, and the widgets and tkinter variables in one are completely invisible to the other. You're creating the IntVar in the first root window, and then trying to associate it with a checkbutton in a second root window. This cannot work. You should always only have a single instance of Tk in a tkinter program.
because of variable scope
try to put CheckVar1 = IntVar() inside the class
use it with self like this
self.CheckVar1 = tk.IntVar() # object of int
self.CheckVar1.set(1) # set value
variable=self.CheckVar1 # passing to the checkbutton as parameter
state = self.CheckVar1.get() # getting value
I'm running Python 2.7.9 on a Mac. I've been unable to figure out why it is when I run my programs that only the Entry Widgets highlight each time I hit the Tab key to move to the next widget. Following is some test code. When I run the script and hit the Tab key, the first entry field is highlighted. The next time I hit the Tab key, the second entry field is highlighted. However, when I hit the tab key to move to the Button Widget, the Button is receiving the focus but there is not highlight to visually indicate to the user the focus.
The OptionMenu widget is skipped altogether, which is also a mystery. Both the radiobutton and the checkbox receives focus, just like the button widget, and again no highlight is present.
I've tried a variety of .config() arrangements to no avail. What am I missing?
from tkinter import *
class App:
def __init__(self, master):
frame = Frame(master)
frame.grid()
#Tests to make sure that Button receives focus.
def yup(self):
print "yup"
entry1 = Entry(frame)
entry1.pack()
entry2 = Entry(frame)
entry2.pack()
button1 = Button(frame, text="Test")
button1.pack()
button1.bind('<Return>', yup)
var1 = IntVar()
c = Checkbutton(frame, text="Expand", variable=var1)
c.pack()
var2 = StringVar()
radio = Radiobutton(frame, text="Test", variable=var2, value=1)
radio.pack()
var3 = StringVar()
optionmenu1 = OptionMenu(frame, var3, "one", "two", "three")
optionmenu1.pack()
root = Tk()
root.geometry('400x400+0+0')
app = App(root)
root.mainloop()
It sounds like you need to configure OS X for "Full Keyboard Access" to allow Tab to focus on all UI controls (versus just text boxes and lists).
In Yosemite (10.10), this setting can be found under System Preferences > Keyboard > Shortcuts, and can be toggled with Control+F7. Note that has nothing to do with Python, and will occur system-wide.
EDIT
So after doing some more testing, there appears to be some issues with the actual highlighting of certain widgets using tk on a Mac. Below is a condensed version of your original sample with some minor modifications for simplicity.
import Tkinter as tk
import ttk # more on this in a minute
class App(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.master = master
entry1 = tk.Entry(self)
entry1.pack()
entry2 = tk.Entry(self)
entry2.pack()
button1 = tk.Button(self, text="Test", command=self.yup)
button1.pack()
def yup(self):
print("yup")
# ...
root = tk.Tk()
app = App(root).pack()
root.mainloop()
With full keyboard access enabled as previously mentioned, I can verify that the button does indeed receive focus: on the third tab, after the first two entry widgets, hitting <space> "clicks" the button and prints to stdout. However there is no visual indication that the button is selected.
Changing the button from a tk.Button to a ttk.Button "fixes" this, and does indeed show the normal "selection" frame around the button when tabbing through the UI.
button1 = tk.Button(self, text="Test", command=self.yup)
# change this to:
button1 = ttk.Button(self, text="Test", command=self.yup)
I have no idea why this is, and I don't know what the consensus about tkinter.ttk is, but I prefer ttk to "plain" tk as it seems to produce widgets which appear more "native" to OS X in my experience. Note I also removed the bind statement, and am reporting my result using the OS X default of space to activate UI elements with full keyboard access enabled.
More on ttk here. Note also that not all tk widgets have a ttk implementation, and that there are also some ttk widgets which do not exist in tk.
Lastly below find the "ttk" version of the original snippet.
import Tkinter as tk
import ttk
class App(ttk.Frame):
def __init__(self, master):
ttk.Frame.__init__(self, master)
self.master = master
entry1 = ttk.Entry(self)
entry1.pack()
entry2 = ttk.Entry(self)
entry2.pack()
button1 = ttk.Button(self, text="Test", command=self.yup)
button1.pack()
def yup(self):
print("yup")
# ...
root = tk.Tk()
app = App(root).pack()
root.mainloop()
Hope this helps.
Try changing the background and highlightbackground colors as below but the problem is possibly because of the way the program is indented --> run the second code block.
top=Tk()
## active background
Button(top, text="Quit", bg="lightblue", activebackground="orange",
command=top.quit).grid(row=1)
top.mainloop()
##--------------- note the 3 lines that have been changed ---------
class App:
## function was not indented
def __init__(self, master):
frame = Frame(master)
frame.grid()
entry1 = Entry(frame)
entry1.pack()
entry1.focus_set()
entry2 = Entry(frame)
entry2.pack()
button1 = Button(frame, text="Test")
button1.pack()
## function called incorrectly
button1.bind('<Return>', self.yup)
var1 = IntVar()
c = Checkbutton(frame, text="Expand", variable=var1)
c.pack()
var2 = StringVar()
radio = Radiobutton(frame, text="Test", variable=var2, value=1)
radio.pack()
var3 = StringVar()
optionmenu1 = OptionMenu(frame, var3, "one", "two", "three")
optionmenu1.pack()
Button(frame, text="Quit", bg="orange", command=master.quit).pack()
## function indented too far
#Tests to make sure that Button receives focus.
def yup(self, args):
print "yup"
root = Tk()
root.geometry('400x400+0+0')
app = App(root)
root.mainloop()