I used Tkinter in Python3 to create a GUI and one of the functions is a pop up window after a button click. This is how my popup window looks like this. The pop up window will take the user inputs and update a variable from the class object (self). Everything works fine except the part in the function UpdateValues() as shown in the code below.
After inputting and updating the Frequency (variable freq in the code) and Iterations (variable loop in code) values in the popup window, they fail to print on the terminal and give a blank screen. Thus the self.freq and self.loop are unable to get updated.
I'm a newbie to python-classes and tkinter and have been struggling with this problem for a while now. Any help will be much appreciated. Thank you!
CODE:
'''Class and other attributes here'''
# Blinking light ssh command
self.freq = "500"
self.loop = "10"
def RunCode():
cmd = "sshpass -p \"raspberry\" ssh -to StrictHostKeyChecking=no pi#pi ./led "+self.freq+" "+self.loop
os.system(cmd)
def PopUpBox1():
popup = tk.Tk()
popup.title("Edit Parameters")
w = 325
h = 75
x = (ws/2) - w/2
y = (hs/2) - h/2
popup.geometry('%dx%d+%d+%d' % (w,h,x,y))
popup.resizable(False,False)
# Adding widgets to PopUpBox
# Modified Button Click Function for Update
def UpdateValues():
update.configure(text='Updated')
print(freq.get())
print(loop.get())
self.freq = str(freq.get())
self.loop = str(loop.get())
popup.destroy()
# Frequency and Iterations label
ttk.Label(popup, text="Frequency:",font='200').grid(column=0, row=0, padx=4, pady=4, sticky='w')
ttk.Label(popup, text="Iterations:",font='200').grid(column=1, row=0, padx=4, pady=4, sticky='w')
# Adding a Textbox Entry widget for frequency values
freq = tk.StringVar()
name_entered = tk.Entry(popup, width=12, textvariable=freq)
name_entered.grid(column=0, row=1, padx=4, pady=4)
# Adding a Combobox for iteration values
loop = tk.StringVar()
number_chosen = ttk.Combobox(popup, width=12, textvariable=loop)
number_chosen['values'] = (1, 2, 4, 42, 100)
number_chosen.grid(column=1, row=1, padx=4, pady=4)
number_chosen.current(0)
# Adding a Button for Updation
update = ttk.Button(popup, text="Update", command = UpdateValues)
update.grid(column=2, row=1, padx=4, pady=4)
# Place cursor into name Entry
name_entered.focus()
Related
I am having button and on pressing it I want to create new Button and new Label.
Label must have random color and must change it on pressing this button to another random color.
My code even can not add buttons correctly, there is problems with placing new(sizes are strange).
How can I improve this? And how can I later create func for new buttons which will change their label's colours, cause I dont have label's names.
import random
from tkinter import *
def color(*args):
pass
def dump( *args):
global count
Butt = Button(root, text="color ", command=color)
Butt.config(width=int(root.winfo_width() / 10), height=int(root.winfo_height() / 10))
Butt.grid(row=0, column=count)
Txt = Label(root, text="Color", bg="#" + ("%06x" % random.randint(0, 16777215)))
Txt.config(width=int(root.winfo_width() / 10), height=int(root.winfo_height() / 10))
Txt.grid(row=1, column=count)
count+=1
root.mainloop()
count=2
TKroot = Tk()
TKroot.title("Hello")
root = Frame(TKroot)
root.place(relx=0, rely=0, relheight=1, relwidth=1)
root.columnconfigure(0, weight=10)
root.columnconfigure(1, weight=10)
root.rowconfigure(0, weight=10)
root.rowconfigure(1, weight=10)
Butt = Button(root, text="Butt ON")
Butt.bind('<Button-1>', dump)
Butt.config(width=int(root.winfo_width() / 10), height=int(root.winfo_height() / 10))
Butt.grid(row=0, column=0)
Exit = Button(root, text="Quit!", command=root.quit)
Exit.config(width=int(root.winfo_width() / 10), height=int(root.winfo_height() / 10))
Exit.grid(row=0, column=1)
Txt = Label(root, text="This is a label", bg="PeachPuff")
Txt.grid(row=1, column=1, columnspan=1)
TKroot.mainloop()
print("Done")
I see a few issues with your code.
1st is you are using place for your frame.
This is going to cause issues when adding new buttons as it will not allow the window to resize correctly with the new layout.
2nd is how you are writing your code. You name your frame root and use the quit method on the frame and not on your actually root window. The way you are writing things makes it harder to follow so consider following PEP8 guidelines when writing your code.
3rd you are trying to apply mainloop to your frame in the dump function. You only ever need 1 instance of mainloop and this applies to the actual root window (Tk()).
To address your question on how to change the label color later on I would use a list to store your buttons and labels. This way we can reference their index values and apply your random color code to the labels on button click.
I have re-written most of your code to follow PEP8 and done some general clean up.
Let me know if you have any questions.
import tkinter as tk
import random
def color(ndex):
button_label_list[ndex][1].config(bg="#%06x" % random.randint(0, 16777215))
def dump():
global count, button_label_list
button_label_list.append([tk.Button(frame, text="color", command=lambda x=count: color(x)),
tk.Label(frame, text="Color", bg="#" + ("%06x" % random.randint(0, 16777215)))])
button_label_list[-1][0].grid(row=0, column=count, sticky='nsew')
button_label_list[-1][1].grid(row=1, column=count, sticky='nsew')
frame.columnconfigure(count, weight=1)
count += 1
root = tk.Tk()
count = 0
button_label_list = []
root.title("Hello")
root.rowconfigure(1, weight=1)
root.columnconfigure(2, weight=1)
frame = tk.Frame(root)
frame.rowconfigure(1, weight=1)
frame.grid(row=0, column=2, sticky='nsew', rowspan=2)
tk.Button(root, text="butt ON", command=dump).grid(row=0, column=0, sticky='nsew')
tk.Button(root, text="Quit!", command=root.quit).grid(row=0, column=1, sticky='nsew')
tk.Label(root, text="This is a label", bg="PeachPuff").grid(row=1, column=1, columnspan=1, sticky='nsew')
root.mainloop()
Results:
A window that can add new buttons and be able to change colors on each label. The main 2 buttons the window starts with are static in that they cannot be pushed out of the window like in you code example and will remain on the left anchored in place.
below an object oriented version.
Every time you press on Color button, you create a new label and a new button
and put label reference in a dictionary.
The color of the label is randomly generate.
After creation if we click on a new button we change the relative label color.
The coolest part of the script is:
command=lambda which=self.count: self.change_color(which)
lambda funcion it's used to keep a reference to the button and label just
create when we call the change_color function.
import tkinter as tk
import random
class App(tk.Frame):
def __init__(self,):
super().__init__()
self.master.title("Hello World")
self.count = 0
self.labels = {}
self.init_ui()
def init_ui(self):
self.f = tk.Frame()
w = tk.Frame()
tk.Button(w, text="Color", command=self.callback).pack()
tk.Button(w, text="Close", command=self.on_close).pack()
w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=0)
self.f.pack(side=tk.LEFT, fill=tk.BOTH, expand=0)
def callback(self):
text_label = "I'm the {} label".format(self.count)
text_button = "I'm the {} button".format(self.count)
color = "#" + ("%06x" % random.randint(0, 16777215))
obj = tk.Label(self.f, text=text_label, bg=color)
obj.pack()
self.labels[self.count]=obj
tk.Button(self.f,
text=text_button,
command=lambda which=self.count: self.change_color(which)).pack()
self.count +=1
def change_color(self,which):
color = "#" + ("%06x" % random.randint(0, 16777215))
self.labels[which].config(bg=color)
def on_close(self):
self.master.destroy()
if __name__ == '__main__':
app = App()
app.mainloop()
I am trying to get the value of from a combobox in tkinter using python 3.6, i been looking to many tutorials but i don't see the problem yet.
every time i press the button don't show anything.
but also there is not errors.
so to clarify ... I am trying to get the value of the tk.combobox when i press ttk.Button.
thank you in advance for any ideas or comments.
this is what i have so far.
import tkinter as tk
from tkinter import ttk
def combo_box_updater():
total_location = ['linden', 'mineola', 'brooklyn']
return total_location
def start_analisys(event=None):
site = jobsite_name.get()
print(site)
# this is part of a definition that automatically will update the names in later versions
job_site = combo_box_updater()
# basic gui setup
unified = tk.Toplevel()
unified.title('Unified 1 Week Timesheet')
unified.configure(background="#00012f")
unified.geometry("650x200")
unified.resizable(width=False, height=False)
entry_width = 30
# basic frame
frame1 = tk.Frame(unified)
frame1.grid(row=0, column=0, sticky='w')
# combo box in the fourth row
jobsite_name = tk.StringVar()
combo_box = ttk.Combobox(frame1, font="none 12 bold", width=20, textvariable=jobsite_name, text="choose location")
combo_box.grid(row=0, column=1, sticky="wesn")
combo_box['values'] = [x for x in job_site]
# Left button side
ttk.Button(frame1, text='Run', command=start_analisys, ).grid(row=0, column=2, sticky='nsew', rowspan=3)
unified.mainloop()
Made three minor edits to your code: added a label to display the result, added a line to combo box setup, and changed the creation of the main window.
import tkinter as tk
from tkinter import ttk
def combo_box_updater():
total_location = ['linden', 'mineola', 'brooklyn']
return total_location
def start_analisys(event=None):
site = jobsite_name.get()
aLabel["text"] = site
print(site)
# this is part of a definition that automatically will update the names in later versions
job_site = combo_box_updater()
# basic gui setup
unified = tk.Tk()
unified.title('Unified 1 Week Timesheet')
unified.configure(background="#00012f")
unified.geometry("650x200")
unified.resizable(width=False, height=False)
entry_width = 30
# basic frame
frame1 = tk.Frame(unified)
frame1.grid(row=0, column=0, sticky='w')
# combo box in the fourth row
jobsite_name = tk.StringVar()
combo_box = ttk.Combobox(frame1, font="none 12 bold", width=20, textvariable=jobsite_name)
combo_box.grid(row=0, column=1, sticky="wesn")
combo_box['values'] = [x for x in job_site]
combo_box.current(0)
# Left button side
ttk.Button(frame1, text='Run', command=start_analisys, ).grid(row=0, column=2, sticky='nsew', rowspan=3)
# add a label
aLabel = ttk.Label(frame1, text='My Label')
# place the label
aLabel.grid(column=3, row=0)
unified.mainloop()
if __name__ == '__main__':
pass
When you add the values as an afterthought like that you need to add the corresponding commands as well. It's much better to add the values through the init method so the commands are automatically added:
jobsite_name = tk.StringVar(value="choose location")
combo_box = ttk.Combobox(frame1, textvariable=jobsite_name, values=job_site, font="none 12 bold", width=20)
I have a window to browse a folder containing necessary files. I am using tkFileDialog for the same. I want to set the value of Entry widget equal to this selected folder. Initially when no folder is selected it will be null. As soon as I select the folder, the path of the selected folder should appear in the Entry widget. The user should be able to modify.Below mentioned is the code for the same.
from Tkinter import *
from tkFileDialog import *
class Checkit:
root = Tk()
#default string to be displayed in the entry of path
path_to_file = StringVar(root, value="abc")
def __init__(self):
self.inputDetail()
def inputDetail(self):
#copy the root window
master = self.root
#create frame for details in the root window
details_frame = Frame(master)
details_frame.grid(row=0, column=0)
#Create the Labels
papercode_label = Label(details_frame, text="Paper code:")
subject_label = Label(details_frame, text="Subject:")
chapter_label = Label(details_frame, text="Chapter:")
batch_label = Label(details_frame, text="Batch:")
ansFolder_label = Label(details_frame, text="Folder containing answer-keys:")
#create entry for the labels
papercode_entry = Entry(details_frame)
subject_entry = Entry(details_frame)
chapter_entry = Entry(details_frame)
batch_entry = Entry(details_frame)
ansFolder_entry = Entry(details_frame)
#create button to add path
path = Button(details_frame, text="Browse", command= lambda: self.addpath(details_frame))
#button to enter the next window
next = Button(details_frame, text="Next", command= lambda: self.checkOmr(details_frame, master))
#Use grid layout to place labels and entry
papercode_label.grid(row=0, sticky=W)
papercode_entry.grid(row=1, sticky=W)
subject_label.grid(row=2, sticky=W)
subject_entry.grid(row=3, column=0, sticky=W)
chapter_label.grid(row=4, sticky=W)
chapter_entry.grid(row=5, column=0, sticky=W)
batch_label.grid(row=6, sticky=W)
batch_entry.grid(row=7, column=0, sticky=W)
ansFolder_label.grid(row=8, sticky=W)
path.grid(row=9, sticky=W, columnspan=2)
next.grid(row=10, sticky=E, columnspan=2)
master.mainloop()
def checkOmr(self, old_frame, master):
#destoy the old frame
old_frame.destroy()
#create frame for details in the root window
inputPath_frame = Frame(master)
inputPath_frame.grid(row=0, column=0)
#create label to input folder containing
omrFolder_label = Label(inputPath_frame, text="Folder containing OMR sheet to be checked:")
#create button to add path
path = Button(inputPath_frame, text="Browse", command= lambda: self.addpath(inputPath_frame))
selected_path = Entry(inputPath_frame, textvariable=self.path_to_file)
#place the label and button on the grid
omrFolder_label.grid(row=0, sticky=W)
path.grid(row=1, column=0, sticky=W)
selected_path.grid(row=1, column=1, sticky=W)
#master.mainloop()
def addpath(self, details_frame):
self.path_to_file = askdirectory(parent=details_frame,initialdir="/",title='Please select a directory')
if __name__=='__main__':
handle = Checkit()
Here I am trying to change the modifying the self. path_to_file value on the click of the button. I tried to print the value of self.path_to_file value in addpath(). It gives correct result there but the value in the Entry selected_path in checkOMR() does not modify.
Can somebody suggest what changes should I make to make that thing possible.
Look at this line:
self.path_to_file = askdirectory(...)
Before this line of code runs, self.path_to_file is an instance of a StringVar. After this line of code has run, self.path_to_file is reset to be just a string.
Assuming you want self.path_to_file to remain an instance of StringVar, you need to change that line to this:
path = askdirectory(...)
self.path_to_file.set(path)
I'm having a problem with the grid alignment when I add in a text box.
I've set up a simple GUI with a 3 column wide display. As part fo the GUI I have a text box for entering notes, and a spinbox for a defined number range.
If I remove the text box from the active code my spinbox aligns correctly with all other entries, but when I place the text box in the grid my spinbox is moved over to the right.
Doesn't seem to matter where the spinbox code is in relation to the text box, once the text box is present the spin box moves over to the right.
At this stage I just can't see what is causing the problem.
I'll admit I'm new to Tkinter (and stackoverflow).
This is the particular line of code that causes the problem:
self.newText.grid(row=display_row, column=0, columnspan=4, padx=10, pady=10).
Sample code below. If I comment out the above line the spin box aligns correctly, if its present then the spinbox is offset. Its only the spinbox that is affected, neither Label, Entry, RadioButton or Text widgets are affected, but Spinbox is. These are the only widgets needed for my GUI.
Any help appreciated.
#-------------------------------------------------------------------------------
# Name: MyGUI
# Purpose: Problem Sample
#
#-------------------------------------------------------------------------------
from Tkinter import *
#===============================
# GUI Class
#===============================
class GUIClass():
'''
GUI class
'''
def __init__(self):
self.displayframe = 0
self.NameVar = StringVar()
self.NumVar = StringVar() # shouldn't be but it works
self.ButtonVar = BooleanVar()
#===============================
# Function for standard button
def StandardEntry(self, display_row,labeltext,entryvar):
'''
Standard 'Entry' feature for GUI.
'''
Label(self.displayframe, text=labeltext).grid(row=display_row, column=0, sticky=W)
name = Entry(self.displayframe, textvariable=entryvar)
name.grid(row=display_row, column=1, sticky=W)
display_row+=1
def BooleanRadioButton(self, display_row, labeltext, entryvar):
'''
Boolean true/false radio button function
'''
Label(self.displayframe, text=labeltext).grid(row=display_row, column=0, sticky=W)
ButtonOn = Radiobutton(self.displayframe, text="Enabled", variable=entryvar, value=True)
ButtonOn.grid(row=display_row, column=1, sticky=W)
ButtonOn = Radiobutton(self.displayframe, text="Disabled", variable=entryvar, value=False)
ButtonOn.grid(row=display_row, column=2, sticky=W)
def StandardSpinBox(self, display_row, labeltext, min_value, max_value, variablename):
'''
Standard spinbox for this project.
'''
Label(self.displayframe, text=labeltext).grid(row=display_row, column=0, sticky=W)
spinboxwidget = Spinbox(self.displayframe, from_=min_value, to=max_value, textvariable=variablename)
spinboxwidget.grid(row=display_row, column=1)
def AddFreeTextForm(self, display_row, notes):
'''
Standard widget for free text entry
'''
self.newText = Text(self.displayframe, width=50, height=8, takefocus=0)
self.newText.grid(row=display_row, column=0, columnspan=4, padx=10, pady=10)
def movesettingstoclass(self, dataclass):
dataclass.NameVar = self.NameVar.get()
dataclass.NumVar = int(self.NumVar.get())
dataclass.ButtonVar = self.ButtonVar.get()
def populate(self, dataclass):
'''
Takes the data in the data structure and populates the GUI.
'''
self.NameVar.set(dataclass.NameVar)
self.NumVar.set(str(dataclass.NumVar))
self.ButtonVar.set(dataclass.ButtonVar)
#===============================
# Data Class
#===============================
class DataClass():
def __init__(self):
self.NameVar = 'SomeName'
self.NumVar = 11
self.ButtonVar = False
def showvalues(self):
'''
Debug function/method for checking values are set correctly.
'''
msg += 'NameVar : %s\n' %self.NameVar
msg += 'NumVar : %d\n' %self.NumVar
msg += 'KVVar : %d\n' %self.KVVar
if self.ButtonVar == True:
msg += 'ButtonVar: True\n'
else:
msg += 'ButtonVar: False\n'
print msg
#=============================================
# Main window
#=============================================
root = Tk()
MyData = DataClass()
mainGUI = GUIClass()
root.title('Problem Example')
menu = Menu(root)
root.config(menu=menu)
#==============================================================================
# text fields to be entered by the user...
display_row = 0
Notes = ''
mainGUI.displayframe = Frame(root)
#==============================================================================
# Some entry input parameter
mainGUI.StandardEntry(display_row, "Some Label", mainGUI.NameVar)
display_row+=1
#==============================================================================
# Some spinbox input parameter
mainGUI.StandardSpinBox(display_row, "Some Label", 3, 21, mainGUI.NumVar)
display_row+=1
#==============================================================================
# Some Radiobutton input parameter
SwitchOn = mainGUI.BooleanRadioButton(display_row, "Button Label", mainGUI.ButtonVar)
display_row+=1
Label(mainGUI.displayframe, text="Configuration Notes").grid(row=display_row, column=1, sticky=W)
display_row += 1
#notes = mainGUI.AddFreeTextForm(display_row, "Configuration Notes", MyData.Notes)
notes = mainGUI.AddFreeTextForm(display_row, Notes)
display_row+=1
mainGUI.displayframe.pack(anchor='w')
#==============================================================================
# and the magic all happens here
mainloop()
You forgot to set the sticky parameter when calling the spinbox' grid method.
spinboxwidget.grid(row=display_row, column=1, sticky=W)
I am making a chat program and decided to use Tkinter for the interface.
What I wanna do is a breeze in C# but Tkinter is new to me.
Basically I have a form with a Entry control and a Text control.
I want to know how to append text from the Entry control to the Text control after the user presses Enter.
Here's my code so far:
from tkinter import *
class Application:
def hello(self):
msg = tkinter.messagebox.askquestion('title','question')
def __init__(self, form):
form.resizable(0,0)
form.minsize(200, 200)
form.title('Top Level')
# Global Padding pady and padx
pad_x = 5
pad_y = 5
# create a toplevel menu
menubar = Menu(form)
#command= parameter missing.
menubar.add_command(label="Menu1")
#command= parameter missing.
menubar.add_command(label="Menu2")
#command= parameter missing.
menubar.add_command(label="Menu3")
# display the menu
form.config(menu=menubar)
# Create controls
label1 = Label(form, text="Label1")
textbox1 = Entry(form)
#command= parameter missing.
button1 = Button(form, text='Button1')
scrollbar1 = Scrollbar(form)
textarea1 = Text(form, width=20, height=10)
textarea1.config(yscrollcommand=scrollbar1.set)
scrollbar1.config(command=textarea1.yview)
textarea1.grid(row=0, column=1, padx=pad_x, pady=pad_y, sticky=W)
scrollbar1.grid(row=0, column=2, padx=pad_x, pady=pad_y, sticky=W)
textbox1.grid(row=1, column=1, padx=pad_x, pady=pad_y, sticky=W)
button1.grid(row=1, column=2, padx=pad_x, pady=pad_y, sticky=W)
form.mainloop()
root = Tk()
Application(root)
So you're using a tkinter.Text box, which supports the .insert method. Let's use it!
def __init__(self,form):
# Lots of your code is duplicated here, so I'm just highlighting the main parts
button1 = Button(form, text='Button1', command = self.addchat)
self.textbox = textbox1 # to make it accessible outside your __init__
self.textarea = textarea1 # see above
form.bind("<Return>", lambda x: self.addchat())
# this is the magic that makes your enter key do something
def addchat(self):
txt = self.textbox.get()
# gets everything in your textbox
self.textarea.insert(END,"\n"+txt)
# tosses txt into textarea on a new line after the end
self.textbox.delete(0,END) # deletes your textbox text