Tkinter button throwing "unknown option -XXXXX" when setting options. Python 2.7 - python

Here is the include file
import tkinter as tk
from tkinter import ttk
class CollapsiblePane(ttk.Frame):
"""
-----USAGE-----
collapsiblePane = CollapsiblePane(parent,
expanded_text =[string],
collapsed_text =[string])
collapsiblePane.pack()
button = Button(collapsiblePane.frame).pack()
"""
def __init__(self, parent, expanded_text ="Collapse <<",
collapsed_text ="Expand >>"):
ttk.Frame.__init__(self, parent)
# These are the class variable
# see a underscore in expanded_text and _collapsed_text
# this means these are private to class
self.parent = parent
self._expanded_text = expanded_text
self._collapsed_text = collapsed_text
# Here weight implies that it can grow it's
# size if extra space is available
# default weight is 0
self.columnconfigure(1, weight = 1)
# Tkinter variable storing integer value
self._variable = tk.IntVar()
# Checkbutton is created but will behave as Button
# cause in style, Button is passed
# main reason to do this is Button do not support
# variable option but checkbutton do
self._button.config( width=1, height=1, borderwidth = 0)
self._button.grid(row = 0, column = 0)
# This wil create a seperator
# A separator is a line, we can also set thickness
self._separator = ttk.Separator(self, orient ="horizontal")
self._separator.grid(row = 0, column = 1, sticky ="we")
self.frame = ttk.Frame(self)
# This will call activate function of class
self._activate()
def toggle(self):
"""Switches the label frame to the opposite state."""
self._variable = not self._variable
def _activate(self):
if not self._variable:
# As soon as button is pressed it removes this widget
# but is not destroyed means can be displayed again
self.frame.grid_forget()
# This will change the text of the checkbutton
self.toggle()
elif self._variable:
# increasing the frame area so new widgets
# could reside in this container
self.frame.grid(row = 1, column = 0, columnspan = 1)
self.toggle()
Here is the program that utilizes it.
# Importing tkinter and ttk modules
from tkinter import *
from tkinter.ttk import *
# Importing Collapsible Pane class that we have
# created in separate file
from collapsiblepane import CollapsiblePane as cp
# Making root window or parent window
root = Tk()
root.geometry('200x200')
# Creating Object of Collapsible Pane Container
# If we do not pass these strings in
# parameter the the defalt strings will appear
# on button that were, expand >>, collapse <<
cpane = cp(root, 'Expanded', 'Collapsed')
cpane.grid(row = 0, column = 0)
# Button and checkbutton, these will
# appear in collapsible pane container
b1 = Button(cpane.frame, text ="GFG").grid(
row = 1, column = 2, pady = 10)
cb1 = Checkbutton(cpane.frame, text ="GFG").grid(
row = 2, column = 3, pady = 10)
mainloop()
And here's my error.
TclError: unknown option "-borderwidth"
The error comes from this line
self._button.config( width=1, height=1, borderwidth = 0)
Which I've also tried as
self._button = ttk.Button(self,command = self._activate, width=1, height=1, borderwidth = 0)
With the same error.
I've removed the borderwidth, and then it throws the same error with width/height. I've tested the width/height/borderwidth in other instances and it works, I'm just not sure why it won't work here.

When using ttk widgets, you have to keep in mind that many of the parameters you would set separately for each tkinter widget are mostly managed by styles when it comes to ttk widgets.
If you look up the ttk.Button widget options (here), you can easily find out which parameters can and can not be set.
In your case, you'd have to create a style for the button.
s = ttk.Style()
s.configure("TButton", borderwidth=0)
If you use this code, you won't have to separately declare that your button is using that style, however every other (ttk) button will automatically use this style too. If you want to set a style for just one button, you'd have to do something like this:
s = ttk.Style()
s.configure("b1.TButton", borderwidth=0) #you can rename b1 to anything you want
and then
b1 = Button(cpane.frame, text ="GFG", style="b1.TButton").grid(
row = 1, column = 2, pady = 10)
You can also read up on styles by yourself here.

Related

Labels Added via Button Command Not Expanding Frame

I am trying to dynamically add labels to a frame contained within a canvas, for scrollbar capability. The labels are being added via a function that is called from a button. The function works fine if called on startup, the frame updates as expected and I have scrollbar capabilities. If I call the function from the button command the frame updates with the labels, but only up to the limit of the starting frame/canvas size. The additional area of the frame containing the rest of the labels won't be visible as the scrollbar isn't "activated"
I tried searching but couldn't find this question being asked before. I am new to Python and Tkinter so apologies if I'm missing something obvious.
from tkinter import *
from tkinter import ttk
root = Tk()
root.geometry("550x550")
main_frame = Frame(root)
main_frame.grid(row = 0, column = 0)
button_frame = Frame(root)
button_frame.grid(row = 1, column = 0)
image_canvas_0 = Canvas(main_frame, height = 500, width = 500)
image_canvas_0.grid(row = 0, column = 0)
image_canvas_0_scrollbar = ttk.Scrollbar(main_frame, orient = VERTICAL, command = image_canvas_0.yview)
image_canvas_0_scrollbar.grid(column = 1, row = 0, sticky = (N,S))
image_canvas_0.config(yscrollcommand = image_canvas_0_scrollbar.set)
image_canvas_0.bind('<Configure>', lambda e: image_canvas_0.configure(scrollregion = image_canvas_0.bbox("all")))
second_frame = Frame(image_canvas_0)
image_canvas_0.create_window((0,0), window = second_frame, anchor = 'nw')
def test_function(*args):
for i in range(100):
label_text = 'test' + str(i)
Label(second_frame, text = label_text).grid(row = i, column = 0)
func_button = Button(button_frame, text = 'Click Me', command = test_function)
func_button.grid(row = 0, column = 0)
#test_function()
root.mainloop()
You aren't changing the scrollregion after you've added widgets to the frame. The canvas doesn't know that there's new data that should be scrollable.
Normally this is done by adding a bind to the frame's <Configure> event, since that event fires whenever the frame changes size.
second_frame.bind("<Configure>", lambda e: image_canvas_0.configure(scrollregion = image_canvas_0.bbox("all")))
The reason your code seems to work when calling the function directly at startup is that you have a similar binding on the canvas itself, which automatically fires once the widget is actually shown on the screen. That happens after you call test_function() when mainloop first starts to run. Once the program starts, the canvas <Configure> event doesn't fire a second time.

Why is my image appearing on the wrong window in Tkinter?

So I am trying to make a text-based game, but also want to incorporate images into it, I have a main menu, a window that is always open, and then a game window, that should have the image in, but for some reason, it appears in the menu window instead. Has anyone got any idea why?
def menu():
master = Tk()
master.geometry("350x300")
master.wm_iconbitmap("zombie.ico")
master.configure(background = '#484a2c')
master.title("Menu")
def game():
master = Tk()
master.geometry("800x500")
master.title("Game")
master.wm_iconbitmap("zombie.ico")
master.configure(background = '#484a2c')
image = PhotoImage(file="Kitchenimage.gif")
label5 = Label(image=image)
label5.image = image
label5.pack()
label = Label(master, text = "Zombie Mansion", background = '#484a2c',
font = ("AR CHRISTY", 30))
label.pack()
b = Button(master,text = "Play Game", height = 1, width = 10)
b.config(background = '#484a2c', activebackground = '#484a2c', font =
("AR CHRISTY", 14), command = get_username)
b.place(x = 120, y = 70)
mainloop()
"Why is my image appearing on the wrong window in Tkinter?"
Assuming you have another instance of Tk as what you refer to as main menu somewhere in your code that you're not showing us like:
main = Tk()
in addition to master = Tk() in your game method, then it's because when you instantiate widgets without passing a parent widget explicitly like in:
label5 = Label(image=image)
then the widget's parent defaults to the Tk instance whose mainloop is being run, in above case supposedly main's. By default widgets are shown under their parents, hence the reason.
Pass parent explicitly like:
label5 = Label(master=main, image=image)
or
label5 = Label(master, image=image)
In most cases, if not all cases, you shouldn't have multiple instances of Tk. If you require additional windows, please use Toplevel widget.

How do I create multiple similar frames with tkinter using classes?

How would I create multiple frames that have the same widgets in Tkinter? Basically what I want to do is create 15 copies of a set of multiple frames that all contain the same widgets as shown in the image, the purpose of this program is to assist the user in sorting photographs into groups based on a specific ID supplied by the user. The radio buttons are there for the user to classify each photo, i.e front, back ,top etc..
Its not very efficient to copy the code 15 times and I want to know if it's possible to use a class to define the frame once and reuse the code for each new frame. I need to keep track of what the user does on each frame and save their selections on the radio buttons and check boxes for each frame. After all the photos have been classified by the user, a button is clicked that should then save all the photos with a new ID and also saves the info from the radio buttons into a csv file. Then the next batch of photos is loaded and the process repeats.
I have included an example of the code I used to create one of the frames, this is the code that I want to make reusable. I do not want to have to repeat it 15 times.
############################################################################
#FRAME 3
Photo_2 = Frame(master, bg = "white",relief = RIDGE, bd = 2)
Photo_2.grid(column = 2, row = 1, padx=5, pady=5)
Lbl2 = Label(Photo_2,text = 'Frame 3')
Lbl2.grid(row = 0, column = 0, columnspan = 4, pady = 5)
# Check box
varc2 = StringVar()
varc2.set(0)
Check_2 = Checkbutton(Photo_2, variable = varc2, text="Relevant?", command = lambda:Chk_Val(varc2))
Check_2.grid(row = 1,column = 0,columnspan = 4)
# Photo 1
P2 = "Photo_2.jpg"
P2 = Image.open(P2).resize((200, 200), Image.ANTIALIAS)
phot2 = ImageTk.PhotoImage(P2)
panel = Label(Photo_2, image = phot2)
panel.grid(columnspan = 3, column=1)
# Create Multiple Radio Buttons
Rad_Cont = Frame(Photo_2)
Rad_Cont.grid(column = 0, row = 2)
v2 = StringVar()
v2.set("Address")
for text,mode in RADIO:
b = Radiobutton(Rad_Cont, text=text, variable=v2,
value=mode, command = lambda:Rad_Val(v2))
b.pack()
################################################################################
Of course it is possible to create a class to represent similar objects.
Here is how I might implement what you're trying to accomplish:
import tkinter as tk
class PhotoFrame(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master, bg='white', relief='ridge', bd=2)
self.label_widget()
self.checkbox_widget()
self.photo_widget()
self.radio_widgets()
def label_widget(self):
self.title_label = tk.Frame(self, text='Frame 3') # Or Frame 1, 2 etc.
self.title_label.grid(row=0, column=0, columnspan=4, pady=5)
def checkbox_widget(self):
self.varc = tk.StringVar()
self.varc.set(0)
self.checkbox = tk.Checkbutton(self, variable=self.varc,
text='Relevant?', command=self.Chk_Val)
self.checkbox.grid(row=1, column=0, columnspan=4)
def photo_widget(self):
# ... Your code here
def radio_widgets(self):
# ... Your code here
def Chk_Val(self):
# ... Your code here
Now I tried not to give you the entire solution so you can learn and figure the rest out by yourself, but I'm sure you can see what I'm getting at in terms of using a class. Now this class PhotoFrame could be used as many times as you'd like, although please understand you'll have to configure each frame appropriately, e.g. I would omit the the text attribute assignment in the label_widget section (You don't want all of your frames titled 'Frame 3'), so in your main program logic, you'd configure each instance of a PhotoFrame object like so:
frame1 = PhotoFrame(master)
frame1.title_label.configure(text='Frame 1') # Along with any other configuration
I hope this all helps - if you are not familiar with classes check out the provided documentation, and here is a great tkinter reference: NMT tkinter

Issue with Combobox

excuse the greeness. Trying to build GUI where option selected from Combobox populates text box. Nothing happening. First time programming so appreciate i have made a lot of errors here.
import tkinter as tk
from tkinter import ttk
from tkinter import scrolledtext
# function to display course selected
def courseDisplay():
box = course.get()
print(box)
# Create instance
win = tk.Tk()
win.resizable(130,130)
win.title("RaceCourse GUI")
# create combobox
course = tk.StringVar()
courseChosen = ttk.Combobox(win,width=60,textvariable=course,state='readonly')
courseChosen['values'] = ("Choose a course","Ascot", "Bath", "Chester")
courseChosen.grid(column=5, row=1,rowspan = 3, columnspan = 3,padx = 300, pady = 40)
courseChosen.current(0)
courseChosen.bind("<<ComboboxSelected>>", courseDisplay)
# create scrolled Text control
scrolW = 46
scrolH = 10
box = scrolledtext.ScrolledText(win, width=scrolW, height=scrolH, wrap=tk.WORD)
box.grid(column=5, row=8, columnspan=3,padx = 300,pady = 10)
# Start GUI
win.mainloop()
Since function courseDisplay is called when some event occurs on combobox (namely, when some option is selected), it should accept one variable (usually called event). So, your function should look like this:
def courseDisplay(event=None):
box = course.get()
print(box)
Of course, You should add another logic for showing test in textbox instead of print.

bring window to top level

I want to present several questions, one after another. The first question is shown as I like, with the cursor set in the entry field. Then I destroy the window and call the function again to create a new window. This time the window is not shown in the front and therefore I first have to click on the screen in order to have the cursor set to the entry field. Also the escape key does not work until I click on the screen to bring the window to the top. I'd be very happy for your help!
Thank you in advance!
Here's my code:
from Tkinter import *
def text_input_restricted(fn,question, nr_letters, limit, len_min, len_max,keys, justify):
class MyApp():
def validate(root, S):
return all(c in keys for c in S)
def __init__(self, q= None):
#save response after "next"-button has been clicked
def okClicked():
lines = e.get()
if len_min < len(lines) < len_max:
lines = unicode(lines).encode('utf-8')
datFile = open(fn, "a")
datFile.write(" '%s'"%(lines))
datFile.close()
self.root.destroy()
self.root = Tk()
vcmd = (self.root.register(self.validate), '%S')
#quit if escape-key has been pressed
self.root.bind('<Escape>', lambda q: quit())
#colors
color = '#%02x%02x%02x' % (200, 200, 200)
self.root.configure(bg=color)
#set window size to screen size
RWidth=MAXX
RHeight=MAXY
self.root.geometry(("%dx%d")%(RWidth,RHeight))
#remove buttons (cross, minimize, maximize)
self.root.overrideredirect(1)
#remove title
self.root.title("")
#item
labelWidget = Label(self.root,text=question, font=("Arial", int(0.02*MAXX)), bd=5, bg=color, justify="center")
labelWidget.place(x=0, y=RHeight/40,width=RWidth)
#"next"-button
ok_width = RWidth/15
ok_height = RWidth/15
okWidget = Button(self.root, text= "next", command = okClicked, font=("Arial",int(0.015*MAXX)), bd=5, justify="center")
okWidget.place(x=RWidth/2-ok_width/2,y=13*RHeight/40, width=ok_width,height=ok_height)
def callback(sv):
c = sv.get()[0:limit]
sv.set(c)
sv = StringVar()
width=nr_letters * int(0.02*MAXX)*1.3
sv.trace("w", lambda name, index, mode, sv=sv: callback(sv))
e = Entry(self.root, textvariable=sv,font=("Arial", int(0.02*MAXX)),justify=justify,validate="key", validatecommand=vcmd)
e.place(x=RWidth/2-width/2, y=9*RHeight/40, width=width)
#show cursor
e.focus_set()
self.root.mainloop()
MyApp()
MAXX=1366
MAXY=768
fn = "D:/test.dat"
text_input_restricted(fn = fn, question=u"f for female, m for male", nr_letters=1, limit =1, len_min =0, len_max=2, keys = 'fm', justify="center")
text_input_restricted(fn = fn, question="How old are you?", nr_letters=2,limit=2, len_min = 1, len_max = 3, keys = '1234567890',justify="center")
In Tk you use the raise command to bring a window to the front of the Z-order. However, raise is a keyword in Python so this has been renamed to lift. Provided your application is still the foreground application you can call the lift() method on a toplevel widget. If the application is not the foreground application then this will raise the window but only above other windows from the same application. On Windows this causes the taskbar icon for your application to start flashing.
You might do better to destroy the contents of the toplevel and replace them. Or even better - create a number of frames holding each 'page' of your application and toggle the visibility of each frame by packing and pack_forgetting (or grid and grid forget). This will avoid loosing the focus completely - you can just set the focus onto the first widget of each frame as you make it visible.

Categories