import os
import tkinter as tk
from tkinter import *
from tkinter.ttk import *
root = Tk()
variable = StringVar(root)
variable = StringVar(root)
variable.set('GB')
w = Combobox(root, values = choices)
w.pack(); root.mainloop()
choices = ['']
msedgelocation= 'my msedge location'
chromelocation= 'my chrome location'
if os.path.exists(msedgelocation):
# add msedge to choices
else:
if os.path.exists(chromelocation):
# add chrome to choices
This will only add 1 option, How do I change the script so it adds multiple options?
Preferably just code and an explanation, but anything helps. Thanks!
EDIT: Also a submit box would be nice
I improved Your code a bit:
from tkinter import Tk, StringVar
from tkinter.ttk import Combobox
import os
def get_choice(event):
os.startfile(choices[event.widget.get()])
root = Tk()
variable = StringVar()
variable.set('GB')
choices = {'GB': 'gb.exe', 'AUS': 'aus.exe', 'USA': 'usa.exe', 'Chrome': 'chrome.exe'}
c_box = Combobox(root, values=list(choices.keys()), textvariable=variable, state='readonly')
c_box.pack()
c_box.bind('<<ComboboxSelected>>', get_choice)
root.mainloop()
First of I got all the info about the combobox here where it was put in simple words.
The other thing is that You should name variables with meaningful variable names not just one letter names, especially if You plan on using the variable.
Also as I mentioned in the comments, it is suggested that You import only what You need, as You can see in my provided code.
Lastly, as #acw1668 already mentioned in comments the code after .mainloop() will only get executed when the window closes.
Related
I'm trying to use two dialogs to get manual input, and then work with that data.
All source I've found claim I should use the get() function, but I wrote a simple mini program yet, and I can't make the second dialog work.
I hope someone can tell me what I'm doing wrong. Here's a file:
from tkinter import *
from tkinter.filedialog import askdirectory
from tkinter import messagebox
def getpath():
def selectPath():
path_ = askdirectory()
path.set(path_)
root = Tk()
root.title('select path')
path = StringVar()
def close():
if(path.get()==""):
messagebox.showinfo("","nothing")
else:
root.withdraw()
root.quit()
Label(root,text="path:").grid(row=0,column=0)
Entry(root,textvariable = path).grid(row=0,column=1)
Button(root,text="select",command=selectPath).grid(row=0,column=2)
Button(root,text="enter",command=close).grid(row=0,column=3)
root.mainloop()
return path.get()
def getname():
def get_need_name():
name = need_name.get()
print('hereherehere'+name) #does not work
root = Tk()
root.title('select name')
need_name = StringVar()
Label(root,text="name:").grid(row=0,column=0)
entry = Entry(root,bd=10,textvariable=need_name)
entry.grid(row=0,column=1)
Button(root,text="enter", font=16, bg="silver", relief='groove', command=get_need_name).grid(row=0,column=2)
root.mainloop()
return name.get()
def main():
path = getpath()
print("mypath:"+path)
print('******************')
print('done!')
name = getname()
print("myname:"+name)
if __name__ == '__main__':
main()
This give me two dialogs I can type in, but only the first dialog works.
The reason is that you are creating multiple instances of Tk, and you don't destroy the instances when you are done with them. This causes two problems. First is a memory leak. Each time you call one of these functions you create a new window and a new tcl interpreter.
The second problem is that the first root window becomes the default window when a root isn't specified. When you create a StringVar in the second function, because you didn't specify which root window it belongs to it will be assigned to the first root window. When you use it as the target of textvariable in a second instance of Tk, tkinter thinks the variable doesn't exist so it creates a new one for the second window. However, your reference is still to the one created in the first root window and is never updated by user input in the second window.
Confusing? Yes, which is why you typically shouldn't be creating more than one instance of Tk.
To make your code work with as few changes as possible and to remove the memory leak caused by not destroying the windows, you can change the last couple of lines in your method to look like the following. This destroys the root window when you are done with it, removing the memory leak and the side effect of having more than one root window.
root = Tk()
...
root.mainloop()
value = path.get()
root.destroy()
return value
The second dialog should look similar:
root = Tk()
...
root.mainloop()
value = name.get()
root.destroy()
return value
This retrieves the value after mainloop exits but before the underlying tcl interpreter is deleted, and then destroys the window and its tcl interpreter.
The next time you create an instance of Tk, that instance will become the new default root, and any new instance of StringVar will go to that root.
Another solution would be to specify the master for the StringVar instance, but that leaves the memory leak in place so it's only half of a solution.
Arguably a better solution is to create a single root window, and either reuse it or create instances of Toplevel rather than Tk. Effbot has some decent documentation on how to create a modal window with wait_window.
After some testing, and googling the root.quit() is the problem
here is a working example for you to look at and mess with.
from tkinter import *
from tkinter.filedialog import askdirectory
from tkinter import messagebox
root = Tk()
path = StringVar()
def select_path():
#uses the return value to set no need to create an additional variable
path.set(askdirectory())
def close():
if path.get() == "":
messagebox.showinfo("","Please select path")
else:
get_name_frame.tkraise()
def get_name():
print("hereherehere", name.get())
get_path_frame = Frame(root)
get_path_frame.grid(row=0, column=0, sticky="nsew")
Label(get_path_frame,text="path:").grid(row=0,column=0)
Entry(get_path_frame,textvariable = path).grid(row=0,column=1)
Button(get_path_frame,text="select",command=select_path).grid(row=0,column=2)
Button(get_path_frame,text="enter",command=close).grid(row=0,column=3)
get_name_frame = Frame(root)
get_name_frame.grid(row=0, column=0,sticky="nsew")
Label(get_name_frame, text="name: ").grid(row=0, column=0)
name = StringVar()
entry = Entry(get_name_frame, bd=10, textvariable = name)
entry.grid(row=0, column=1)
Button(get_name_frame,text="enter", font=16, bg="silver", relief='groove', command=get_name).grid(row=0,column=2)
get_path_frame.tkraise()
root.mainloop()
How do I copy Entry widget text and paste into another Entry widget in the same window.
i.e. Let's imagine you are completing a joint credit application but you and the co-applicant have the same mailing address. Instead of re-typing the same address over again, there should be a checkbutton on the application that when it's checked it will auto-populate the co-applicant's mailing address with the main applicant's address. How do I get this done in tkinter? (I'm a tkinter and python rookie)
thanks in advance DP
There can probably a couple more ways of achieving the behavior you want. I think using the textvariable option and Variable Classes fits well here. Together, they let a widget's text to be the same as another at all times. With the Checkbutton the user decides whether or not to do that:
try: # In order to be able to import tkinter for
import tkinter as tk # either in python 2 or in python 3
except ImportError:
import Tkinter as tk
def on_selection(copy_widget, paste_widget, condition_widget):
if condition_widget.var.get():
paste_widget['textvariable'] = copy_widget['textvariable']
else:
paste_widget['textvariable'] = ''
def create_entry_widgets(master):
entries = list()
for i in range(2):
entries.append(tk.Entry(master))
entries[-1].pack()
entries[0].var = tk.StringVar()
entries[0]['textvariable'] = entries[0].var
return entries
def create_checkbutton(master, entries):
checkbutton = tk.Checkbutton(master, text="Copy?")
checkbutton.var = tk.BooleanVar(value=False)
checkbutton.config(variable=checkbutton.var, onvalue=True, offvalue=False)
checkbutton['command'] = lambda cw=entries[0], pw=entries[1], \
cdw=checkbutton: on_selection(cw, pw, cdw)
checkbutton.pack()
return checkbutton
def main():
root = tk.Tk()
entries = create_entry_widgets(root)
checkbutton = create_checkbutton(root, entries)
tk.mainloop()
if __name__ == '__main__':
main()
I am building my first larger Python application using tkinter, python 3.6.3 and window OS. The GUI for the application consists of a Notebook with several tabs. Each of the tabs in turn contains a Labelframe which in turn contains a number of other widgets.
When searching Stackflow, I found the idea to lets each labelFrame be a class. Thereafter import the class in the main.py and finally creating an instance of the class.
Now when pressing the button 'Start' in tab1 I would like to execute the 'printThis' function. Ideally I would like to use the function defined in the script main.py. It would also be interested in knowing how to call the 'printThis' method withing the Run_Test_Window class. Unfortunately I have not solved either problem.
Interestingly the program actually prints "Now successful" without that I do anything but when I press the 'Start'-button nothing happens.
Grateful for help! Thanks!
main.py
import tkinter as tk
import tkinter.ttk as ttk
import RunTestClass as RT
def printThis():
print('Successful')
root = tk.Tk()
note = ttk.Notebook(root)
note.grid()
tab1 = ttk.Label(note, width = -20)
note.add(tab1, text = " Run Test ")
window1 = RT.Run_Test_Window(tab1)
root.mainloop()
RunTestClass.py
import tkinter as tk
import tkinter.ttk as ttk
# from main import printThis
class Run_Test_Window:
def printThis(self):
print('Now successful!')
def __init__(self,tab1):
runTestLabelFrame = ttk.LabelFrame(text ="Run Test", padding =10)
runTestLabelFrame.grid(in_ = tab1, padx = 20, pady = 20)
self.startButton = ttk.Button(runTestLabelFrame, text="START",command=self.printThis())
self.startButton.grid(row=5, column=1, padx = 10)
If I'm correct you want the button to use the printThis() from main. This can be done by adding the following in your main:
rt = RT.Run_Test_Window(tab1)
rt.startButton.configure(command=printThis)
To call the printThis() defined in RunTestClass, use (also in main)
rt.printThis()
Note: leave the brackets when creating the button in the command argument. So change it to this:
self.startButton = ttk.Button(runTestLabelFrame, text="START",command=self.printThis)
im new topython 2.7 and want to know if it is possible to open a tkinter messagebox with a button combination on keyboard (Ctrl+alt+'something')
that pops up like an windows error message
import win32api
import time
import math
import Tkinter
import tkMessageBox
top = Tkinter.Tk()
def Message():
tkMessageBox.showinfo("Window", "Text")
for i in range(9000):
x = int(600+math.sin(math.pi*i/100)*500)
y = int(500+math.cos(i)*100)
win32api.SetCursorPos((x,y))
time.sleep(.01)
Yes, you can bind to control and alt characters. Bindings are fairly well documented. Here's one good source of information:
http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm
As an example, to bind to ctrl-alt-x you would do this:
top.bind("<Control-Alt-x>", Message)
You can bind to a sequence of events by specifying the whole sequence. For example, if you wanted to implement a cheat code you could do something like this:
label.bind("<c><h><e><a><t>", Message)
For letters, "a" is the same as "<a>", so you can also do this:
label.bind("cheat", Message)
Here is a complete working example:
import Tkinter as tk
import tkMessageBox
def Message(event=None):
tkMessageBox.showinfo("Window", "Text")
def Cheat(event=None):
tkMessageBox.showinfo("Window", "Cheat Enabled!")
root = tk.Tk()
label = tk.Label(root, text="Press control-alt-m to see the messagebox\ntype 'cheat' to enable cheat.")
label.pack(fill="both", expand=True, padx=10, pady=100)
label.bind("<Control-Alt-x>", Message)
label.bind("<c><h><e><a><t>", Cheat)
label.focus_set()
root.mainloop()
If you want something like: Press button A, then press button B then open a Message box it is possible.
Do something like:
from Tkinter import *
import tkMessageBox
def change():
global switch
switch=True
def new_window():
if switch:
tkMessageBox.showinfo("Random name", "Correct combination")
else:
print "Not the correct order"
root = Tk()
switch = False
root.bind("<A>", change)
root.bind("<B>",new_window)
root.mainloop()
If you want more buttons then use an integer and increase it while using switches for the correct button order.
Note that you can bind key combinations as well with root.bind("<Shift-E>") for example
Edit: Now a and b keyboard button insted of tkinter buttons
I am trying to change a program's behaviour on the fly by editing its source file (under Python 3.4.2, Windows 8.1). My program is a tkinter GUI with a button that sums two values, but I want to be able to change the button's behaviour. I am currently trying to do this by editing the source file (changing, say, the addition to subtraction), saving it, and then clicking a button whose callback function imports the source file. I want my code changes to be reflected in the running GUI without having to exit and restart the program. I also want this import to only recompile the lines that I changed, rather than the entire file.
The program, reload0.py:
import time
import serial
import sys
import os
import tkinter as tk
from tkinter import ttk
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
try:
import Tkinter # Python 2
import ttk
except ImportError:
import tkinter as Tkinter # Python 3
import tkinter.ttk as ttk
mGui = Tk()
mGui.title("GUI")
mGui.geometry('400x200+100+100')
def mOp():
num1 = value1.get()
num2 = value2.get()
Op=num1+num2
name1.set('Sum')
name2.set(Op)
def mReLoad():
import reload0.py
mGui.update()
def mCheck():
if len(name1.get()) == 0:
name1.set('name1')
mGui.update()
if (len(name2.get()) == 0):
name2.set('name2')
mGui.update()
try:
print(value1.get())
except ValueError:
value1.set(0)
mGui.update()
try:
print(value2.get())
except ValueError as ValE:
value2.set(0)
mGui.update()
print(ValE)
value1 = DoubleVar()
value2 = DoubleVar()
name1 = StringVar()
name2 = StringVar()
mButtonSave = Button(mGui, text = "Operation", command = mOp, fg = 'Red').place(x=150,y=80)
mButtonLoad = Button(mGui, text = "ReLoad Operation", command = mReLoad, fg = 'Red').place(x=150,y=110)
mButtonLoad = Button(mGui, text = "Check", command = mCheck, fg = 'Red').place(x=150,y=140)
tText1 = Entry(mGui, textvariable = name1).place(x=10,y=80)
tText2 = Entry(mGui, textvariable = name2).place(x=10,y=100)
vText1 = Entry(mGui, textvariable = value1).place(x=10,y=120)
vText2 = Entry(mGui, textvariable = value2).place(x=10,y=140)
For your purpose of changing button functionality there are easy ways than changing source code, and as commented it's not possible.
judging from another question i saw of yours you are quite new to python programming. I would recommend spending some time on some basic tutorials getting to know python and some programming concepts first.
for instance
import reload.py
should simply be
import reload
https://wiki.python.org/moin/BeginnersGuide/NonProgrammers
read a book, do some examples, and so enough you will be the one answering the questions here, good luck!