使用Python和TKinter,如何重绘主屏幕

问题描述

|| 我有一个程序,其中主窗口分为两个部分,每个部分都有一组表单(简单的标签/输入列)。认值为第一部分中的4列,第二部分中的2。我希望用户能够更改此比例。我想我已经完成了所有编程工作,但是我无法以正确的结构重新绘制主窗口。任何帮助将非常感激。谢谢。 我喜欢您的示例背后的简单性和想法,但是,我正在修改一些旧代码,没有时间将布局重写为定义。我试图创建一个可以简单地执行grid_columnconfig()的def,但是没有用。我减少了代码,使其看起来像我正在使用的代码,并且也可以正常工作。如果您更改变量\'max_pol_modules \',则会调整左侧的列和右侧的列,因此,我正在尝试通过界面小部件更改此变量并重绘。
from Tkinter import *
import tkFont

max_pol_modules = 3
max_bus_modules = 6 - max_pol_modules

tech_green = \'#5E9732\'
button_grey = \'#666666\'
grey = \'#777777\'

def config1():
    global max_pol_modules,max_bus_modules
    max_pol_modules = 1
    max_bus_modules = 6 - max_pol_modules
#    print max_bus_modules

#    left_frame.update()


def config2():
    global max_pol_modules,max_bus_modules
    max_pol_modules = 2
    max_bus_modules = 6 - max_pol_modules
#    print max_bus_modules


def config3():
    global max_pol_modules,max_bus_modules
    max_pol_modules = 3
    max_bus_modules = 6 - max_pol_modules
#    print max_bus_modules


def config4():
    global max_pol_modules,max_bus_modules
    max_pol_modules = 4
    max_bus_modules = 6 - max_pol_modules
#    print max_bus_modules

def about():
    Box.showinfo(\"About GUI\",\"GE Bus Converter and Point of Load GUI\")

def bar(frame,row,span):
    \"create a bar to separate a section\"
    x = Frame(frame,relief=GROOVE,bd=2,width=86*(span+1),height=2)
    x.grid(row=row,column=0,columnspan=10,pady=7,sticky=S+E)
    x.grid_propagate(0)

def bar2(frame,pady=3,sticky=S+E)
    x.grid_propagate(0)

def bar3(frame,width=100,sticky=S+E)
    x.grid_propagate(0)

root = Tk()

menubar = Menu(root)

submenu=Menu(menubar,tearoff=0)

submenu2=Menu(submenu,tearoff=0)
submenu2.add_command(label=\"1 - 5\",command=config1)
submenu2.add_command(label=\"2 - 4\",command=config2)
submenu2.add_command(label=\"3 - 3\",command=config3)
submenu2.add_command(label=\"4 - 2\",command=config4)

submenu_help = Menu(submenu,tearoff=0)
submenu_help.add_command(label=\"About\",command=about)


submenu.add_cascade(label=\"Change Configuration\",menu=submenu2)

submenu.add_command(label=\"Exit\",command=root.quit)

menubar.add_cascade(label=\"Settings\",menu=submenu)
menubar.add_cascade(label=\"Help\",menu=submenu_help)

# display the menu
root.config(menu=menubar)

entry_wid = 6
small_font = tkFont.Font(family=\'Arial\',size=8,weight=\'bold\')
lite_font = tkFont.Font(family=\'Arial\',size=9)
large_font = tkFont.Font(family=\'Arial\',size=9)
heading_font = tkFont.Font(family=\'Arial\',size=10,weight=\'bold\')
button_font = tkFont.Font(family=\'Arial\',weight=\'bold\')
root.option_add(\'*font\',lite_font)
root.option_add(\'*background\',\'#C2C2C4\')
root.option_add(\'*Label.font\',small_font)
root.option_add(\'*Entry.background\',\'white\')
root.option_add(\'*Button.font\',button_font)
root.option_add(\'*Button.background\',button_grey)
root.option_add(\'*Button.foreground\',\'yellow\')
root.option_add(\'*Text.background\',\'white\')
root.option_add(\'*Text.font\',small_font)
root.option_add(\'*ScrolledText.font\',lite_font) 

left_frame = Frame(root)
right_frame = Frame(root)

pol_frame = Frame(left_frame,relief=SUNKEN)
x = Label(pol_frame,text=\"POL Address\",anchor=E)
x.grid(row=0,sticky=E)

x = Label(pol_frame,text=\"Rtrim (Kohms)\",anchor=E)
x.grid(row=1,text=\"Nominal Vout (V)\",anchor=E)
x.grid(row=2,sticky=E)

bar2(pol_frame,max_pol_modules)

module_address = []
module_i2c = []
module_status = []
module_resistor = []
module_vout_nominal = []
for i in range(max_pol_modules):
    # Module ID and address
    f = Frame(pol_frame)
    x = Label(f,text=i+1)
    x.grid(row=0,column=0)
    v = StringVar()
    x = Entry(f,textvariable=v,width=3,justify=CENTER)
    x.grid(row=0,column=1)
    f.grid(row=0,column=i+1,pady=8,padx=20)
    module_address.append(v)
    module_i2c.append(\"\")
    module_status.append(0)
    # module resistor
    v = StringVar()
    x = Entry(pol_frame,width=entry_wid,justify=CENTER)
    f = lambda event,module=i: change_resistor_event(event,module)
    g = lambda value,m=i,o=16: set_change(value,m,o)
    x.bind(\"<keyrelease>\",f,\"+\")
    x.bind(\"<keyrelease>\",g,\"+\")
    x.bind(\"<FocusOut>\",\"+\")
    x.grid(row=1,pady=0)
    module_resistor.append(v)
    # module nominal vout
    v = StringVar()
    x = Label(pol_frame,width=entry_wid-1,relief=SUNKEN,bg=\'#ddddDD\',font=lite_font)
    x.grid(row=2,pady=0)
    module_vout_nominal.append(v)

bus_frame = Frame(left_frame,relief=SUNKEN)
#x = Label(bus_frame,text=\"Module (address)\",anchor=E)
#x.grid(row=0,column=max_pol_modules+1,sticky=E)
x = Label(bus_frame,text=\"Bus Conv Address\",sticky=E)
config_bus = []
r = 0

#for i in range(max_pol_modules,max_pol_modules+max_bus_modules):
for i in range(max_bus_modules):
    # Module ID and address
    f = Frame(bus_frame)
    x = Label(f,text=i+5)
    x.grid(row=0,column=i+2,padx=20)
    module_address.append(v)
    module_i2c.append(\"\")
    module_status.append(0)


bar2(bus_frame,r,max_bus_modules) 
r += 1

# the measured values
measure_info = [\"Vout (V)\",\"IoUt (A)\",\"Vin (V)\",\"Temp (degC)\"]
measures_bus = []
for mi in measure_info:
    x = Label(bus_frame,text=mi,anchor=E)
    x.grid(row=r,sticky=E)
    m = []
    for j in range(max_bus_modules):
        v = StringVar()
        x = Label(bus_frame,font=lite_font)
        x.grid(row=r,column=j+2)
        m.append(v)
    measures_bus.append(m)
    r += 1

pol_frame.grid(row=0,sticky=N+W)

bus_frame.grid(row=0,column=1,sticky=N+W)

left_frame.grid(row=0,sticky=N)

right_frame.grid(row=0,sticky=N)

root.mainloop()
编辑示例,其中需要删除form [4]和form [5]。
import Tkinter as tk

class App(tk.Tk):
    def __init__(self,*args,**kwargs):
        tk.Tk.__init__(self,**kwargs)
        self.forms = []
        self.toolbar = tk.Frame(self)
        self.toolbar.pack(side=\"top\",fill=\"x\",expand=False)
        button2 = tk.Button(self.toolbar,text=\"2 columns\",command=self.layout2col)
        button3 = tk.Button(self.toolbar,text=\"3 columns\",command=self.layout3col)
        button2.pack(side=\"left\")
        button3.pack(side=\"left\")
        self.forms_frame = tk.Frame(self,borderwidth=2,relief=\"groove\")
        self.forms_frame.pack(side=\"top\",fill=\"both\",expand=\"True\",padx=2,pady=2)
        for i in range(6):
            frame = tk.LabelFrame(self.forms_frame,text=\"Form %s\" % i)
            self.forms.append(frame)
            label = tk.Label(frame,text=\"Field %s\" % i)
            entry = tk.Entry(frame,width=20)
            label.pack(side=\"left\",fill=\"y\")
            entry.pack(side=\"left\",expand=True)

        self.layout2col()

    def layout3col(self):
        self.forms[0].grid(column=0,row=0,padx=4,pady=2,sticky=\"ew\")
        self.forms[1].grid(column=0,row=1,sticky=\"ew\")
        self.forms[2].grid(column=1,sticky=\"ew\")
        self.forms[3].grid(column=1,sticky=\"ew\")
        self.forms[4].grid(column=2,sticky=\"ew\")
        self.forms[5].grid(column=2,sticky=\"ew\")
        self.forms_frame.grid_columnconfigure(0,weight=1)
        self.forms_frame.grid_columnconfigure(1,weight=1)
        self.forms_frame.grid_columnconfigure(2,weight=1)

    def layout2col(self):
        self.forms[0].grid(column=0,sticky=\"ew\")


        self.forms_frame.grid_columnconfigure(0,weight=0)

if __name__ == \"__main__\":
    app = App()
    app.mainloop()
    

解决方法

        由于使用的是网格几何管理器,因此只需要使用
grid
方法将它们放置在新的行和列中。您可能还需要调用
rowconfigure
和/或
columnconfigure
将适当的权重附加到新的行和列上。 这是一个显示一般原理的人为例子。它可能会更有效,但是希望它可以给您一个大概的想法:
import Tkinter as tk

class App(tk.Tk):
    def __init__(self,*args,**kwargs):
        tk.Tk.__init__(self,**kwargs)
        self.forms = []
        self.toolbar = tk.Frame(self)
        self.toolbar.pack(side=\"top\",fill=\"x\",expand=False)
        button2 = tk.Button(self.toolbar,text=\"2 columns\",command=self.layout2col)
        button3 = tk.Button(self.toolbar,text=\"3 columns\",command=self.layout3col)
        button2.pack(side=\"left\")
        button3.pack(side=\"left\")
        self.forms_frame = tk.Frame(self,borderwidth=2,relief=\"groove\")
        self.forms_frame.pack(side=\"top\",fill=\"both\",expand=\"True\",padx=2,pady=2)
        for i in range(6):
            frame = tk.LabelFrame(self.forms_frame,text=\"Form %s\" % i)
            self.forms.append(frame)
            label = tk.Label(frame,text=\"Field %s\" % i)
            entry = tk.Entry(frame,width=20)
            label.pack(side=\"left\",fill=\"y\")
            entry.pack(side=\"left\",expand=True)

        self.layout2col()

    def layout3col(self):
        self.forms[0].grid(column=0,row=0,padx=4,pady=2,sticky=\"ew\")
        self.forms[1].grid(column=0,row=1,sticky=\"ew\")
        self.forms[2].grid(column=1,sticky=\"ew\")
        self.forms[3].grid(column=1,sticky=\"ew\")
        self.forms[4].grid(column=2,sticky=\"ew\")
        self.forms[5].grid(column=2,sticky=\"ew\")
        self.forms_frame.grid_columnconfigure(0,weight=1)
        self.forms_frame.grid_columnconfigure(1,weight=1)
        self.forms_frame.grid_columnconfigure(2,weight=1)

    def layout2col(self):
        self.forms[0].grid(column=0,sticky=\"ew\")
        self.forms[2].grid(column=0,row=2,sticky=\"ew\")
        self.forms[4].grid(column=1,sticky=\"ew\")
        self.forms[5].grid(column=1,weight=0)

if __name__ == \"__main__\":
    app = App()
    app.mainloop()
    ,        您可能已经尝试过此方法,但是可以在根窗口小部件上调用update()吗? 首先,您可能还需要再次打包()。     

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...