[Tkinter-discuss] Font change does reflect window size when executed without debugging
Madhu Subramaniam
madhu.subramaniam at gmail.com
Tue Apr 7 11:37:03 CEST 2009
Hi,
Below is an application with a scrollable frame. There is an option to
change the font size of the text, (using tkFont) thereby changing the size
of the widgets and the containers.
this is done by the function 'fontctrl'. The thing i am not able to figure
out is that the change in window size works when i put a breakpoint/debugger
(pdb.set_trace()) and exceute them manually in the shell. But when i run w/o
debugging, then the print statements do not show me the change in window
size.
for example, when the font is medium: self2.winfo_height() shows 840 with
debugging but 600 without the debugger in place.
Python version 2.5.2, Tkinter 8.4, OS: Linux, Ubuntu
-----------------------------------------------------------------
(see code below)
from Tkinter import *
import math
import tkColorChooser
import pdb
import os
import string
import threading
from tkFileDialog import *
from tkMessageBox import *
import sys
import tkFont
def window2(fr1,root):
#Structure
# root window
#-------------------------------------
# |
# |
# |
# fr1 (frame)
#-------------------------------------------------------------------------
# | | |
# | | |
# | | |
# Menu Button can(Canvas) Scrollbars (vertical and horizontal)
#-----------------------------------------------------------------------------------
# | |
# | |
# | |
#menu with radiobuttons fr2(Frame-create_window)
# for font change |
# |
# 2 rows of labels,
optionmenu
def fontctrl( rval,self1,self2, can_fr2=0, can=0,fr1=0):
# Change in size (viewed with help of the print statements) found, only
when implemented with debugging!!,
# if pdb.set_trace() is removed, then the change in size is not
reflected in the print statements!
#pdb.set_trace()
self2.minsize(width = 10, height = 10)
self2.maxsize(width = 4000, height = 2000)
if rval == 1:
self1.configure(size=10)
self2.update_idletasks()
self2.minsize(width = self2.winfo_width(), height = 640)
self2.maxsize(width = self2.winfo_width(), height = 640)
self2.update_idletasks()
print '#----------------------------------'
print 'Schrift G: small'
print 'self2.winfo_height(): ', self2.winfo_height()
print 'fr1.winfo_height(): ', fr1.winfo_height()
print 'can.winfo_height(): ' , can.winfo_height()
print 'can_fr2.winfo_height(): ' , can_fr2.winfo_height()
print '#----------------------------------'
if rval == 2:
self1.configure(size=14)
self2.update_idletasks()
self2.minsize(width = self2.winfo_width(), height = 840)
self2.maxsize(width = self2.winfo_width(), height = 840)
self2.update_idletasks()
print '#----------------------------------'
print 'Schrift G: Medium'
print 'self2.winfo_height(): ', self2.winfo_height()
print 'fr1.winfo_height(): ', fr1.winfo_height()
print 'can.winfo_height(): ' , can.winfo_height()
print 'can_fr2.winfo_height(): ' , can_fr2.winfo_height()
print '#----------------------------------'
if rval == 3:
self1.configure(size=16)
self2.update_idletasks()
self2.minsize(width = self2.winfo_width(), height = 940)
self2.maxsize(width = self2.winfo_width(), height = 940)
self2.update_idletasks()
print '#----------------------------------'
print 'Schrift G: Big'
print 'self2.winfo_height(): ', self2.winfo_height()
print 'fr1.winfo_height(): ', fr1.winfo_height()
print 'can.winfo_height(): ' , can.winfo_height()
print 'can_fr2.winfo_height(): ' , can_fr2.winfo_height()
print '#----------------------------------'
try:
can_fr2.update_idletasks()
can.update_idletasks()
can.config(scrollregion=(0,0,max(can.winfo_width(),
can_fr2.winfo_width()),max(can.winfo_height(), can_fr2.winfo_height())))
scrollx.update_idletasks()
scrolly.update_idletasks()
except:
pass
global content
#font and grid options for fr1
#-------------------------------------------------------------------------------------------------
fonts = tkFont.Font(family="MS Sans Serif",size=12, weight="bold")
fr1.grid_columnconfigure(0,weight=1)
fr1.grid_rowconfigure(1,weight=1)
fr_menu= Frame(fr1, bg= 'turquoise')
fr_menu.grid_propagate(1)
root.grid_rowconfigure(0,weight=1)
root.grid_columnconfigure(0,weight=1)
fr_menu.grid(row=0,column=0, sticky = 'news')
#Canvas, scrollbars
#----------------------------------------------------------------------------------------------------------------------------------------------------------
can= Canvas(fr1, relief='groove')
can.grid(row=1, column=0, sticky = 'news')
can.fr2 = Frame(can, width = 100, height = 50)
var1= StringVar()
var1.set('1')
scrollx= Scrollbar(fr1, orient=HORIZONTAL)
scrolly= Scrollbar(fr1, orient=VERTICAL)
scrollx.config(command=can.xview)
scrolly.config(command=can.yview)
scrollx.grid(row=2,column=0, sticky= S+E+W,columnspan=3)
scrolly.grid(row=0,column=2, sticky= N+S+E, rowspan=2)
can.config(xscrollcommand=scrollx.set)
# Frame fr2 created om the canvas , holding widgets
#----------------------------------------------------------------------------------------------------------------------------------------------------------
font=fonts
om1 = OptionMenu(can.fr2, var1, '1', '2', '3')
lbl = Label(can.fr2, text= content[0], justify = 'left', font=fonts)
lbl1 = Label(can.fr2, text= 'Hello', font=fonts)
lbl2 = Label(can.fr2, text= content[1], justify = 'left', font=fonts)
lbl.grid(row=0, column=0, padx = 15)
lbl1.grid(row=0, column=1, padx = 15)
om1.grid(row=0, column=2, padx = 15)
lbl2.grid(row=0, column=3, padx = 15)
var1_a= StringVar()
var1_a.set('A')
om1_a = OptionMenu(can.fr2, var1_a, 'A', 'B', 'C')
lbl_a = Label(can.fr2, text= content[0], justify = 'left', font=fonts)
lbl1_a = Label(can.fr2, text= 'Hello', font=fonts)
lbl2_a= Label(can.fr2, text= content[1], justify = 'left', font=fonts)
lbl_a.grid(row=1, column=0, padx = 15)
lbl1_a.grid(row=1, column=1, padx = 15)
om1_a.grid(row=1, column=2, padx = 15)
lbl2_a.grid(row=1, column=3, padx = 15)
can.fr2.m1=can.create_window(0,0, window= can.fr2, anchor = 'nw')
print can.coords(can.fr2.m1)
can.config(yscrollcommand=scrolly.set)
can.xview(MOVETO, 0.0)
can.yview(MOVETO, 1.0)
can.fr2.update_idletasks()
can.update_idletasks()
can.config(scrollregion=(0,0,can.winfo_width(),max(can.winfo_height(),
can.fr2.winfo_height())))
can.grid_propagate(0)
can.update_idletasks()
#Menu button and radiobuttons for font change
#---------------------------------------------------
fr_menu_but=Menubutton(fr1, text="Optionen", font = fonts , underline=0)
fr_menu_but.menu = Menu(fr_menu_but, tearoff=0)
fr_menu_but.menu.font = Menu(fr_menu_but.menu, tearoff=0)
rval = IntVar()
rval.set(2)
winsize = ['Small','Medium','Big']
for i,x in enumerate(winsize):
fr_menu_but.menu.font.add_radiobutton(label=x, font = fonts, var=
rval, val=i+1, command = (lambda:
fontctrl(rval.get(),fonts,root,can.fr2,can,fr1)))
fr_menu_but.menu.add_cascade(label="Schrift-Groesse", font = fonts,
menu=fr_menu_but.menu.font, underline=0)
fr_menu_but['menu'] = fr_menu_but.menu
fr_menu_but.update_idletasks()
fr_menu_but.grid(row=0, column=0)
fontctrl(rval.get(),fonts,root,can.fr2,can,fr1)
root.update_idletasks()
#MAIN ROUTINE
#------------------------------------------
global content
content=[ '/xxx/yyyyyy/aaa/bbbb/tttttt/rrrrrrrr/',
'/xxx/yyyyyy/aaa/bbbb/tttttt/rrrrrrrr/',
'/xxx/yyyyyy/aaa/bbbb/tttttt/rrrrrrrr/',
'/xxx/yyyyyy/aaa/bbbb/tttttt/rrrrrrrr/']
root= Tk()
root.tk.call('tk', 'scaling', 1)
fr1= Frame(root, width= 900, height= 600, bg='red')
fr1.grid(row=0,column=0, sticky='news')
fr1.grid_propagate(0)
window2(fr1,root)
root.mainloop()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20090407/d7155cf8/attachment.htm>
More information about the Tkinter-discuss
mailing list