Building a small application for personal use with Python and thought I'd try my hand with a little GUI programming using Tkinter. This is the GUI I've created so far:
Application doubts:
How can I make sure that the three LableFrames - A, B and C in the screenshot - have the same width? (Or rather, have the width equal to the widest of the three? For example, in the screenshot, A is the widest and I'd like B and C to also be as wide - up to the line D).
(It doesn't have to dynamically calculated - it is enough if I can ensure that the width are the same when I code it, the first time round. They don't need to change on runtime.)
Tk Grid Geometry Manager doubts:
When you use frames, is the grid (row, column) specific to only the size of the frame or is it calculated based on the size of the form (root window)?
How is the size of a column determined in a grid?
I haven't fully understood what 'weight' does within a grid. When should it be used?
The Python GUI Code:
import Tkinter
if __name__ == '__main__':
form = Tkinter.Tk()
getFld = Tkinter.IntVar()
form.wm_title('File Parser')
stepOne = Tkinter.LabelFrame(form, text=" 1. Enter File Details: ")
stepOne.grid(row=0, columnspan=7, sticky='W', \
padx=5, pady=5, ipadx=5, ipady=5)
helpLf = Tkinter.LabelFrame(form, text=" Quick Help ")
helpLf.grid(row=0, column=9, columnspan=2, rowspan=8, \
sticky='NS', padx=5, pady=5)
helpLbl = Tkinter.Label(helpLf, text="Help will come - ask for it.")
helpLbl.grid(row=0)
stepTwo = Tkinter.LabelFrame(form, text=" 2. Enter Table Details: ")
stepTwo.grid(row=2, columnspan=7, sticky='W', \
padx=5, pady=5, ipadx=5, ipady=5)
stepThree = Tkinter.LabelFrame(form, text=" 3. Configure: ")
stepThree.grid(row=3, columnspan=7, sticky='W', \
padx=5, pady=5, ipadx=5, ipady=5)
inFileLbl = Tkinter.Label(stepOne, text="Select the File:")
inFileLbl.grid(row=0, column=0, sticky='E', padx=5, pady=2)
inFileTxt = Tkinter.Entry(stepOne)
inFileTxt.grid(row=0, column=1, columnspan=7, sticky="WE", pady=3)
inFileBtn = Tkinter.Button(stepOne, text="Browse ...")
inFileBtn.grid(row=0, column=8, sticky='W', padx=5, pady=2)
outFileLbl = Tkinter.Label(stepOne, text="Save File to:")
outFileLbl.grid(row=1, column=0, sticky='E', padx=5, pady=2)
outFileTxt = Tkinter.Entry(stepOne)
outFileTxt.grid(row=1, column=1, columnspan=7, sticky="WE", pady=2)
outFileBtn = Tkinter.Button(stepOne, text="Browse ...")
outFileBtn.grid(row=1, column=8, sticky='W', padx=5, pady=2)
inEncLbl = Tkinter.Label(stepOne, text="Input File Encoding:")
inEncLbl.grid(row=2, column=0, sticky='E', padx=5, pady=2)
inEncTxt = Tkinter.Entry(stepOne)
inEncTxt.grid(row=2, column=1, sticky='E', pady=2)
outEncLbl = Tkinter.Label(stepOne, text="Output File Encoding:")
outEncLbl.grid(row=2, column=5, padx=5, pady=2)
outEncTxt = Tkinter.Entry(stepOne)
outEncTxt.grid(row=2, column=7, pady=2)
outTblLbl = Tkinter.Label(stepTwo, \
text="Enter the name of the table to be used in the statements:")
outTblLbl.grid(row=3, column=0, sticky='W', padx=5, pady=2)
outTblTxt = Tkinter.Entry(stepTwo)
outTblTxt.grid(row=3, column=1, columnspan=3, pady=2, sticky='WE')
fldLbl = Tkinter.Label(stepTwo, \
text="Enter the field (column) names of the table:")
fldLbl.grid(row=4, column=0, padx=5, pady=2, sticky='W')
getFldChk = Tkinter.Checkbutton(stepTwo, \
text="Get fields automatically from input file",\
onvalue=1, offvalue=0)
getFldChk.grid(row=4, column=1, columnspan=3, pady=2, sticky='WE')
fldRowTxt = Tkinter.Entry(stepTwo)
fldRowTxt.grid(row=5, columnspan=5, padx=5, pady=2, sticky='WE')
transChk = Tkinter.Checkbutton(stepThree, \
text="Enable Transaction", onvalue=1, offvalue=0)
transChk.grid(row=6, sticky='W', padx=5, pady=2)
transRwLbl = Tkinter.Label(stepThree, \
text=" => Specify number of rows per transaction:")
transRwLbl.grid(row=6, column=2, columnspan=2, \
sticky='W', padx=5, pady=2)
transRwTxt = Tkinter.Entry(stepThree)
transRwTxt.grid(row=6, column=4, sticky='WE')
form.mainloop()
(Note: For Python 2.4.3)
If you use the same columnspan and use sticky='WE' on all three LabelFrames then they should have the same width. For example, you want to use
stepTwo = Tkinter.LabelFrame(form, text=" 2. Enter Table Details: ")
stepTwo.grid(row=2, columnspan=7, sticky='WE', \
padx=5, pady=5, ipadx=5, ipady=5)
Questions
1) When you use frames, is the grid (row, column) specific to only the
size of the frame or is it calculated based on the size of the form
(root window)?
There is an interdependence here. The preferred size of the form will depend on the preferred sizes of the children and the layout, but the actual sizes of the children will depend on the actual size of the form and the layout. Layout is done from the children to the root to determine the preferred sizes, and then when it gets to the root, the preferred size is used as the actual size (unless overridden). Layout then goes back down assigning actual sizes.
2) How is the size of a column determined in a grid?
The preferred size of a column is determined based to be the minimum preferred width of all the items in that row. The actual size of the column is determined by the preferred size plus some percentage of the extra space of the parent widget.
3) I haven't fully understood what 'weight' does within a grid. When
should it be used?
The weight determines the percentage of extra space that I mentioned above. The amount of the extra space given to the column is column_weight/total_weight.
Related
My desktop resolution is 1920x1080 and I am developing for an environment which is the same, 1440x900 or less (it depends on user's screen resolution)
When I am on 1920x1080 resolution, all widgets are visible but when the resolution change, some widget are not visible. However I used grid method. What to do?
Here is an example code:
from tkinter import *
import tkinter as tk
root = Tk()
width=1440
height=900
root.geometry('%dx%d+0+0' % (width,height))
print(width, height)
frame1=LabelFrame(root, text="frame1")
frame2=LabelFrame(root, text="frame2")
frame1.pack(fill="both", expand="yes", padx=20, pady=10)
frame2.pack(fill="both", expand="no", padx=20, pady=10)
abc=StringVar()
Label(frame2, text="A").grid(row=1, column=0, padx=5, pady=3)
Entry(frame2, textvariable=abc).grid(row=1, column=1, padx=5, pady=3,sticky="nsew")
Label(frame2, text="B").grid(row=1, column=2, padx=5, pady=3)
Entry(frame2, textvariable=abc).grid(row=1, column=3, padx=5, pady=3,sticky="nsew")
Label(frame2, text="C").grid(row=1, column=4, padx=5, pady=3)
Entry(frame2, textvariable=abc).grid(row=1, column=5, padx=5, pady=3,sticky="nsew")
Label(frame2, text="D").grid(row=1, column=6, padx=5, pady=3)
Entry(frame2, textvariable=abc).grid(row=1, column=7, padx=5, pady=3,sticky="nsew")
Label(frame2, text="E").grid(row=2, column=0, padx=5, pady=3)
Entry(frame2, textvariable=abc).grid(row=2, column=1, padx=5, pady=3,sticky="nsew")
Label(frame2, text="F").grid(row=2, column=2, padx=5, pady=3)
Entry(frame2, textvariable=abc).grid(row=2, column=3, padx=5, pady=3,sticky="nsew")
Label(frame2, text="G").grid(row=2, column=4, padx=5, pady=3)
Entry(frame2, textvariable=abc).grid(row=2, column=5, padx=5, pady=3,sticky="nsew")
Label(frame2, text="H").grid(row=2, column=6, padx=5, pady=3)
Entry(frame2, textvariable=abc).grid(row=2, column=7, padx=5, pady=3,sticky="nsew")
Label(frame2, text="I").grid(row=4, column=0, padx=5, pady=3)
bla=Text(frame2, width=220, height=2.5, font=("Calibri", 14))
bla.grid(row=4, column=1, columnspan=11, padx=5, pady=3, sticky="nsew")
Label(frame2, text="J").grid(row=3, column=0, padx=5, pady=3)
Entry(frame2, textvariable=abc).grid(row=3, column=1, padx=5, pady=3,sticky="nsew")
Label(frame2, text="K").grid(row=3, column=2, padx=5, pady=3)
Entry(frame2, textvariable=abc).grid(row=3, column=3, padx=5, pady=3,sticky="nsew")
Label(frame2, text="L").grid(row=5, column=0, padx=5, pady=3)
Entry(frame2, textvariable=abc).grid(row=5, column=1, padx=5, pady=3,sticky="nsew")
Label(frame2, text="M").grid(row=5, column=2, padx=5, pady=3)
Entry(frame2, textvariable=abc).grid(row=5, column=3, columnspan=2, padx=5, pady=3,sticky="nsew")
Label(frame2, text="N").grid(row=5, column=5, padx=5, pady=3)
Entry(frame2, textvariable=abc).grid(row=5, column=6, padx=5, pady=3,sticky="nsew")
Label(frame2, text="O").grid(row=5, column=7, padx=5, pady=3)
Entry(frame2, textvariable=abc).grid(row=5, column=8,columnspan=3, padx=5, pady=3,sticky="nsew")
root.mainloop()
Thank u
The problem is that you're explicitly setting the text widget width to 220 characters. Depending on the font, that may make the widget wider than 1440 pixels. On my machine, the widget ends up being 1988 pixels. Because of that, it forces the columns to be wider than will fit in the window.
The simple solution in this specific case seems to be to not explicitly set the size of the text widget. Leave it at its natural size and let the geometry manager cause it to expand to fill the window. If you truly need it to be 220 characters wide, you will need to make the font smaller so that that it will fit within the constraints of the window.
As a general rule of thumb, you should set the size of a widget to its minimum required size, and let the geometry managers be in control of making them large enough to fill any extra space.
I'm having trouble getting my first "None" checkbox widget where I want it. Here is a link to a screenshot.
I'm trying to get the "None" checkbox widget to be directly to the right of the Entry widget. The bottom rows containing spinbox widgets are contained within a LabelFrame widget located at row=7, column=2 that has a columnspan of 2. My first checkbox widget is located at row=5, column=3 with sticky='w'. Why then isn't it up against the Entry widget at row=5, column=2 since I gave the LabelFrame a columnspan of 2?
Here is my code:
tk.Label(GUI, text='label 1').grid(row=0, sticky='w')
tk.Label(GUI, text='label 2').grid(row=1, sticky='w')
tk.Label(GUI, text='label 3').grid(row=2, sticky='w')
tk.Label(GUI, text='label 4').grid(row=3, sticky='w')
tk.Label(GUI, text='label 5').grid(row=4, sticky='w')
tk.Label(GUI, text='label 6').grid(row=5, sticky='w')
tk.Label(GUI, text='label 7').grid(row=6, sticky='w')
tk.Label(GUI, text='label 8').grid(row=7, sticky='w')
tk.Label(GUI, text='label 9').grid(row=8, sticky='w')
tk.Label(GUI, text='$').grid(row=2, column=1, sticky='w')
tk.Label(GUI, text='$').grid(row=3, column=1, sticky='w')
tk.Label(GUI, text='$').grid(row=4, column=1, sticky='w')
tk.Label(GUI, text='9999').grid(row=2, column=2, sticky='w')
input1 = tk.Entry(GUI, width=10)
input2 = tk.Entry(GUI, width=10)
input3 = tk.Entry(GUI, width=10)
button_input = tk.Checkbutton(GUI, text='None')
input5 = tk.Entry(GUI, width=10)
date_field = tk.LabelFrame(GUI)
scheduled_start_month_input = tk.Spinbox(date_field, from_=1, to=12, width=2)
scheduled_start_day_input = tk.Spinbox(date_field, from_=1, to=31, width=2)
scheduled_start_year_input = tk.Spinbox(date_field, from_=2018, to=2025, width=4)
scheduled_start_hour_input = tk.Spinbox(date_field, from_=0, to=23, width=2)
scheduled_start_minute_input = tk.Spinbox(date_field, from_=0, to=59, width=2)
start_on_launch_option = tk.Checkbutton(date_field, onvalue=True, offvalue=False, text='On Launch')
scheduled_stop_month_input = tk.Spinbox(date_field, from_=1, to=12, width=2)
scheduled_stop_day_input = tk.Spinbox(date_field, from_=1, to=31, width=2)
scheduled_stop_year_input = tk.Spinbox(date_field, from_=2018, to=2025, width=4)
scheduled_stop_hour_input = tk.Spinbox(date_field, from_=0, to=23, width=2)
scheduled_stop_minute_input = tk.Spinbox(date_field, from_=0, to=59, width=2)
scheduled_stop_none = tk.Checkbutton(date_field, onvalue=True, offvalue=False, text='None')
input1.grid(row=3, column=2, sticky='w')
input2.grid(row=4, column=2, sticky='w')
input3.grid(row=5, column=2, sticky='w')
button_input.grid(row=5, column=3, sticky='w')
input5.grid(row=6, column=2, sticky='w')
date_field.grid(row=7, column=2, rowspan=2, columnspan=2, sticky='w')
scheduled_start_month_input.grid(column=0)
tk.Label(date_field, text='/').grid(column=1, row=0)
scheduled_start_day_input.grid(column=2, row=0)
tk.Label(date_field, text='/').grid(column=3, row=0)
scheduled_start_year_input.grid(column=4, row=0)
tk.Label(date_field, text=' ').grid(column=5, row=0)
scheduled_start_hour_input.grid(column=6, row=0)
tk.Label(date_field, text=':').grid(column=7, row=0)
scheduled_start_minute_input.grid(column=8, row=0)
start_on_launch_option.grid(row=0, column=9)
scheduled_stop_month_input.grid(column=0)
tk.Label(date_field, text='/').grid(column=1, row=1)
scheduled_stop_day_input.grid(column=2, row=1)
tk.Label(date_field, text='/').grid(column=3, row=1)
scheduled_stop_year_input.grid(column=4, row=1)
tk.Label(date_field, text=' ').grid(column=5, row=1)
scheduled_stop_hour_input.grid(column=6, row=1)
tk.Label(date_field, text=':').grid(column=7, row=1)
scheduled_stop_minute_input.grid(column=8, row=1)
scheduled_stop_none.grid(column=9, row=1, sticky='w')
tk.Button(GUI, text='Launch').grid(row=9, sticky='w')
GUI.mainloop()
The problem is that the 2nd column is wider than you think it is. To see it, change the line where you add the input widget to the grid so that it looks like this:
input3.grid(row=5, column=2, sticky='ew')
You'll see that the checkbutton is in fact as far left as it can go, and it is the column to its left that is responsible for all of the extra space.
The reason is because the datefield widget spans two columns, but those two columns aren't wide enough to hold the datefield. grid has to add extra space to the columns so that the widget will fit.
If you want the second column to stay a fixed size and have column 3 to be the one that grows and shrinks, you have to give column 3 a non-zero weight so that grid can allocate all extra space to that column.
You can do that by adding this line of code somewhere:
GUI.grid_columnconfigure(3, weight=1)
Another solution would be to add the weight to an invisible column 4, and then have datefield span three columns instead of two. That will cause columns 2 and 3 to retain their natural size, and any extra space would be given to an empty column.
GUI.grid_columnconfigure(4, weight=1)
date_field.grid(row=7, column=2, rowspan=2, columnspan=3, sticky='w')
Add following code:
GUI.grid_columnconfigure(3, weight=1)
It means that column 3 would occupy all the place it can get. Your/default weight is 0. and that means Checkbutton instance is wide just as "None" text is, so sticky w equals sticky e equals sticky we or wens. If you set the weight to 1 then your sticky argument actually takes place.
I would like to know why the methods SavePVParms Close and ApplyGraph are not defined when I call them through the buttons. I know that if I put them inside __init__ they will work and my problem is solved but I don't understand why it wouldn't work the way it is. I tried to look for this question and found this Python Class: Global/Local variable name not defined and this Python says global name not defined but forgive me if I can't understand the answer there. Could you help me understand why?
Background: This class is a top level window that popups when I press a button in the root window (here i am just showing you guys the toplevel window directly). It is supposed to pass a dictionary of all the entries when I save it (not yet implemented). **Additionally (but not required to answer if you don't want to) is this an adequate style of coding in OOP? (less than 1 week learning tkinter and moving from precedural python to OOP python).
import Tkinter as tk
import ttk
class PVDialog(tk.Toplevel):
def SavePVParms(self):
print "saved"
self.destroy()
def Close(self):
self.destroy()
def ApplyGraph(self):
print 'applied'
def __init__(self, parent):
tk.Toplevel.__init__(self, parent)
print parent
self.title('PV Parameters Configuration')
self.geometry('800x480')
self.resizable(width=False, height=False)
self.columnconfigure(0, weight=1)
self.columnconfigure(1, weight=8)
self.rowconfigure(0, weight=1)
self.rowconfigure(1, weight=2)
# ====== FRAMES =======
lb1Frame = tk.LabelFrame(self, text='Electric Properties')
lb1Frame.grid(row=0, column=0, sticky='ewns', pady=(15, 2), padx=(30,10))
AdvFrame = tk.LabelFrame(self, text='Thermal Properties')
AdvFrame.grid(row=1, column=0, sticky='ewns', pady=5, padx=(30,10))
pcurveFrame = tk.LabelFrame(self, text='PV Curves')
pcurveFrame.grid(row=0, column=1, sticky='ewns', padx=(0,30), pady=(15,5),rowspan=2)
ctrlFrame = tk.Frame(self)
ctrlFrame.grid(row=2, column=0, columnspan=2, sticky='ens', padx=20, pady=(2, 20))
# ======== PARAMETER INPUT DATA FRAME ============= #
labelName = tk.Label(lb1Frame, text='Name', anchor='w')
labelCellType = tk.Label(lb1Frame, text='Cell Type', anchor='w')
labelPower = tk.Label(lb1Frame, text='Rated Power [W]', anchor='w')
labelOV = tk.Label(lb1Frame, text='Open Voltage [V]', anchor='w')
labelSCC = tk.Label(lb1Frame, text='Short-circuit Current [A]', anchor='w')
labelMPC = tk.Label(lb1Frame, text='Maximum Point Current [I]', anchor='w')
labelMPV = tk.Label(lb1Frame, text="Maximum point Voltage [V]", anchor='w')
labelSeries = tk.Label(lb1Frame, text='Cells in series', anchor='w')
labelPallel = tk.Label(lb1Frame, text='Cells in parallel', anchor='w')
labelNOCT = tk.Label(AdvFrame, text='NOCT', anchor='w')
labelThC = tk.Label(AdvFrame, text='Current T factor [%/K]', anchor='w')
labelThV = tk.Label(AdvFrame, text='Voltage T factor [%/K]', anchor='w')
labelThP = tk.Label(AdvFrame, text='Power T factor [%/K]', anchor='w')
labelName.grid(row=0, sticky='ew', padx=10, pady=2)
labelCellType.grid(row=1, sticky='ew', padx=10, pady=2)
labelPower.grid(row=2, sticky='ew', padx=10, pady=2)
labelOV.grid(row=3, sticky='ew', padx=10, pady=2)
labelSCC.grid(row=4, sticky='ew', padx=10, pady=2)
labelMPC.grid(row=5, sticky='ew', padx=10, pady=2)
labelMPV.grid(row=6, sticky='ew', padx=10, pady=2)
labelSeries.grid(row=7, sticky='ew', padx=10, pady=2)
labelPallel.grid(row=8, sticky='ew', padx=10, pady=2)
labelNOCT.grid(row=9 , sticky='ew', padx=10, pady=2)
labelThC.grid(row=10, sticky='ew', padx=10, pady=2)
labelThV.grid(row=11, sticky='ew', padx=10, pady=2)
labelThP.grid(row=12, sticky='ew', padx=10, pady=2)
entryName = ttk.Entry(lb1Frame, show='Default')
entryCellType = ttk.Combobox(lb1Frame)
entryPower = ttk.Entry(lb1Frame)
entryOV = ttk.Entry(lb1Frame)
entrySCC = ttk.Entry(lb1Frame)
entryMPC = ttk.Entry(lb1Frame)
entryMPV = ttk.Entry(lb1Frame)
entrySeries = ttk.Entry(lb1Frame)
entryPallel = ttk.Entry(lb1Frame)
entryNOCT = ttk.Entry(AdvFrame, width=23)
entryThC = ttk.Entry(AdvFrame, width=23)
entryThV = ttk.Entry(AdvFrame, width=23)
entryThP = ttk.Entry(AdvFrame, width=23)
entryName.grid(row=0, column=1, sticky='ew')
entryCellType.grid(row=1, column=1, sticky='ew')
entryPower.grid(row=2, column=1, sticky='ew')
entryOV.grid(row=3, column=1, sticky='ew')
entrySCC.grid(row=4, column=1, sticky='ew')
entryMPC.grid(row=5, column=1, sticky='ew')
entryMPV.grid(row=6, column=1, sticky='ew')
entrySeries.grid(row=7, column=1, sticky='ew')
entryPallel.grid(row=8, column=1, sticky='ew')
entryNOCT.grid(row=9, column=1, sticky='w', padx=(26,0))
entryThC.grid(row=10, column=1, sticky='w', padx=(26,0))
entryThV.grid(row=11, column=1, sticky='w', padx=(26,0))
entryThP.grid(row=12, column=1, sticky='w', padx=(26,0))
# ==== BUTTON COMANDS ======
saveBttn = ttk.Button(ctrlFrame, text='Save', command=SavePVParms)
closBttn = ttk.Button(ctrlFrame, text='Cancel', command=Close)
applyBttn = ttk.Button(ctrlFrame, text='Apply', command=ApplyGraph)
saveBttn.grid()
closBttn.grid(row=0, column=1, padx=10)
applyBttn.grid(row=0, column=2, padx=(0, 10))
# ======== RUNNIG THE CLIENT ============
root = tk.Tk()
dialog = PVDialog(root)
root.mainloop()
Update: As I was making about to submit this question, I remembered that __init__ is the first thing that is read and executed in the Class, so that's why the are defined if I put them in __init__. I still don't understand why python can't call them if they are outside __init__ doesn't python read the whole class? how can I call then self.method that is outside of __init__ then?. Thanks!
SavePVParms, Close, ApplyGraph methods are belong to your class, so you need to use self. to tell interpreter that, you want to use methods from your class.
saveBttn = ttk.Button(..., command=self.SavePVParms)
For coding standarts, you should check PEP 8 but as long as you are consistent with your style, it shouldn't matter that much.
I am little bit comfused with grid system in tkinter Python. Can anyone show how to make it in right way?! ListBox and Label items positions are not in the places where I expexted to see them.
CODE:
self.third_label = Label(self, text="TEXT")
self.third_label.grid(row=2, column=0, columnspan=4, padx=10, pady=10, sticky=W)
self.fourth_label = Label(self, text="LONG TEXT")
self.fourth_label.grid(row=2, column=1, columnspan=4, padx=10, pady=10, sticky=W)
self.fifth_label = Label(self, text="SOME TEXT")
self.fifth_label.grid(row=2, column=2, columnspan=6, padx=10, pady=10, sticky=W)
self.sixth_label = Label(self, text="BIG TEXT")
self.sixth_label.grid(row=2, column=3, columnspan=4, padx=10, pady=10, sticky=W)
self.first_listbox = Listbox(self, width=40, selectmode=EXTENDED)
self.first_listbox.grid(row=3, column=0, columnspan=4, padx=10, pady=10, sticky=W)
self.second_listbox = Listbox(self, width=40, selectmode=EXTENDED)
self.second_listbox.grid(row=3, column=2, columnspan=4, padx=10, pady=10, sticky=W)
self.third_listbox = Listbox(self, width=40, selectmode=EXTENDED)
self.third_listbox.grid(row=3, column=4, columnspan=4, padx=10, pady=10, sticky=W)
self.fourth_listbox = Listbox(self, width=40, selectmode=EXTENDED)
self.fourth_listbox.grid(row=3, column=6, columnspan=4, padx=10, pady=10, sticky=W)
What I have right now:
Just Example:
The grid system works fine. The problem is your columnspans, which don't make much sense. You're gridding the widgets into certain column positions then giving them a columnspan that is beyond the range of where the next widget is to be gridded so on and so forth.
Small example:
import string
import tkinter as tk
root = tk.Tk()
for i in range(3):
tk.Label(root, text=string.ascii_letters).grid(row=0, column=i)
tk.Listbox(root, width=40).grid(row=1, column=i)
root.mainloop()
Edit from comments (for listbox size):
To get the number of lines in a listbox you can use the .size() method.
Image:
I cannot get the state of a (tk.)Label and (tk.)Checkbutton to visually turn to disable and normal depending on the value of an OptionMenu.
The command binding on the OptionMenu below seems fine, but my use of configure(state=...), in updateState() has no visible effect. Should I force some "refresh" or "repaint" (if yes how in the partial code below?) or am I missing something else?
import Tkinter
from Tkinter import Frame, LabelFrame, Label, Entry, Button, StringVar, OptionMenu, Checkbutton, IntVar, DISABLED, NORMAL
class GeneratorDialog:
# The UI to configure the settings by the user
def __init__(self, root, ctrl):
self.__root = root
self.__ctrl = ctrl
def updateState(self, value, label, entry):
if(value.get()==CONST.FORMAT_PDF): # test works, as the dialog below show in alternance as expected
tkMessageBox.showinfo('Info', message="enabling checkbox")
label.configure(state=NORMAL)
entry.configure(state=NORMAL)
else:
tkMessageBox.showinfo('Info', message="disabling the checkbox")
label.configure(state=DISABLED)
entry.configure(state=DISABLED)
#self.__root.update_idletasks() # how to force "redraw" with grid() manager?
def show(self):
self.__root.title(CONST.APP_NAME)
mainFrame = Frame(self.__root)
mainFrame.grid(sticky='ew')
outputFrame = LabelFrame(mainFrame, text='Output Settings')
outputFrame.grid(column=0, row=1, padx=5, pady=5, sticky='ew')
keepGeneratedScribusFilesLabel = Label(outputFrame, text='Keep Scribus Files:', width=15, anchor='e')
keepGeneratedScribusFilesLabel.grid(column=4, row=2, padx=5, pady=5, sticky='e')
keepGeneratedScribusFilesCheckbox = Checkbutton(outputFrame, variable=self.__ctrl.getKeepGeneratedScribusFilesCheckboxVariable(), anchor='w')
keepGeneratedScribusFilesCheckbox.grid(column=5, row=2, padx=5, pady=5, sticky='w')
mergeOutputLabel = Label(outputFrame, text='Merge in Single File:', width=15, anchor='w')
mergeOutputLabel.grid(column=0, row=2, padx=5, pady=5, sticky='w')
mergeOutputCheckbox = Checkbutton(outputFrame, variable=self.__ctrl.getMergeOutputCheckboxVariable())
mergeOutputCheckbox.grid(column=1, row=2, padx=5, pady=5, sticky='w')
outputFormatLabel = Label(outputFrame, text='Output Format:', anchor='e')
outputFormatLabel.grid(column=2, row=2, padx=5, pady=5, sticky='e')
outputFormatListBox = OptionMenu(outputFrame, self.__ctrl.getSelectedOutputFormat(), *self.__ctrl.getOutputFormatList(),
command=lambda l=keepGeneratedScribusFilesLabel, c=keepGeneratedScribusFilesCheckbox, v=self.__ctrl.getSelectedOutputFormat(): self.updateState(v, l, c))
outputFormatListBox.grid(column=3, row=2, padx=5, pady=5, sticky='w')
# Buttons to Cancel or to Run the Generator with the given Settings
helpButton = Button(buttonFrame, text='Help', width=10, command=self.__ctrl.helpButtonHandler)
helpButton.grid(column=0, row=0, padx=5, pady=5, sticky='e')
cancelButton = Button(buttonFrame, text='Cancel', width=10, command=self.__ctrl.buttonCancelHandler)
cancelButton.grid(column=1, row=0, padx=5, pady=5, sticky='e')
generateButton = Button(buttonFrame, text='Generate', width=10, command=self.__ctrl.buttonOkHandler)
generateButton.grid(column=2, row=0, padx=5, pady=5, sticky='e')
# Finally show the Generator Dialog
mainFrame.grid()
self.__root.grid()
self.__root.mainloop()
full code is at https://github.com/berteh/ScribusGenerator/blob/93a12de4be28054344533ad8fc424e37180668de/ScribusGenerator.py#L278
solved it by using class variables (self.X) instead of lambda local variables.
still don't know why the former code (with local variables in the lambda) would not update the status of the Tk elements (maybe it created copies thereof?)... but I found a workaround.
thanks all for your help