2013-04-10 84 views
2
def red(): 
    frame3.output_display.config(fg = 'red', font=root.customFont1) 
def blue(): 
    frame3.output_display.config(fg = 'darkblue', font=root.customFont2) 
def green(): 
    frame3.output_display.config(fg = 'darkgreen',font=root.customFont3) 
def black(): 
    frame3.output_display.config(fg = 'black',font=root.customFont4) 


from tkinter import * 
from tkinter import ttk 
import tkinter.font 
from tkinter.scrolledtext import ScrolledText 

root = Tk() 
root.title("Change Text") 
root.geometry('700x500') 
# change font size and family: not used currently because of resizing issue 
root.customFont1 = tkinter.font.Font(family="Handwriting-Dakota", size=12) 
root.customFont2 = tkinter.font.Font(family="Comic sans MS", size=14) 
root.customFont3 = tkinter.font.Font(family="Script MT", size=16) 
root.customFont4 = tkinter.font.Font(family="Courier", size=10) 

# FRAME 3 
frame3 = LabelFrame(root, background = '#EBFFFF', borderwidth = 2, text = 'text entry and display frame', fg = 'purple',bd = 2, relief = FLAT, width = 75, height = 40) 
frame3.grid(column = 2, row = 0, columnspan = 3, rowspan = 6, sticky = N+S+E+W) 
#frame3.grid_rowconfigure(0, weight=0) 
#frame3.grid_columnconfigure(0, weight=0) 
frame3.grid_propagate(True) 


frame3.output_display = ScrolledText(frame3, wrap = WORD) 
frame3.output_display.pack(side = TOP, fill = BOTH, expand = True) 
frame3.output_display.insert('1.0', 'the text should appear here and should wrap at character forty five', END) 
#frame3.output_display.config(state=DISABLED) # could be used to prevent modification to text (but also prevents load new file) 

# draws all of the buttons, 
ttk.Style().configure("TButton", padding=6, relief="flat",background="#A52A2A", foreground='#660066') 
names_colour=(('Red',red),('Blue',blue),('Green',green),('Black',black)) 
root.button=[] 

for i,(name, colour) in enumerate(names_colour): 
    root.button.append(ttk.Button(root, text=name, command = colour)) 
    row,col=divmod(i,4) 
    root.button[i].grid(sticky=N+S+E+W, row=6, column=col, padx=1, pady=1) 

root.mainloop() 

在GUI中当文本字体的面和字体大小发生更改时,文本框将调整大小并隐藏按钮。在我的天真中,我认为文本框将保持相同的大小,并且文本将简单地包裹在文本框的约束内。至少我想达到什么。显然有一些概念在字体大小或文本框中,tkinter,我不明白 谢谢tkinter文本框调整大小而不是包装文本以填充框

+0

可能的重复http://stackoverflow.com/questions/9833698,虽然它涉及香草文本,答案可以很容易地适应。 – FabienAndre 2013-04-11 11:33:48

+0

谢谢你的评论。我尝试过使用您的参考文献中讨论的方法,但没有成功。我改变了frame3.grid_propagate(True)到frame3.grid_propagate(False),正如我所说没有用。建议将是最受欢迎的。 – user1478335 2013-04-12 09:57:36

回答

0

文本小部件的宽度定义为字符宽度单位而不是像素,它会尝试使用其配置的宽度作为其尽可能最小的宽度。该小部件将更宽的字体,更狭窄的字体。因此,如果你给它一个宽字体,它会尝试使自己变宽以保持X字符宽。

那么,你如何解决这个问题?

一个解决方案是将宽度和高度设置为小。例如,如果将宽度和高度设置为1(一),那么该小部件将只会尝试强制自己成为一个字符宽度和高度。除非您使用绝对巨大的字体,否则在放大字体时几乎看不到该小部件。

然后,您将需要依靠包,网格或放置算法来将小部件拉伸至所需的尺寸。如果您使用网格,通常这意味着您需要确保适当地设置列和行权重,并设置粘性属性。

这样做的缺点是必须确保你的GUI具有正确的大小,而不是根据它的神奇发生基于每个部件的首选大小。

frame3.output_display.configure(width=1, height=1) 
root.grid_rowconfigure(0, weight=1) 
root.grid_columnconfigure(2, weight=1) 

当我运行与上述其他行代码,文本组件仍然是:

作为一个快速的黑客,你可以在程序中通过小部件已经被创建之后加上几行看到这固定的大小和文字在不同的地方与每种字体包装。