Tkinter Dynamic Widget编辑

时间:2016-09-07 13:26:29

标签: python class python-3.x user-interface tkinter

这是我的代码:

class render_window:
    def __init__(self, height, width, window_title):
        self.root_window = Tk()
        w = width
        h = height
        ws = self.root_window.winfo_screenwidth() # width of the screen
        hs = self.root_window.winfo_screenheight() # height of the screen
        x = (ws/2) - (w/2)
        y = (hs/2) - (h/2)
        self.root_window.title(window_title)
        self.root_window.minsize(width, height)
        self.root_window.geometry('%dx%d+%d+%d' % (w, h, x, y))

    def new_button(self, button_text, button_command="", grid_row=0, grid_column=0, grid_sticky="NESW", grid_columnspan=1, grid_rowspan=1):
        self.button = ttk.Button(self.root_window, text=button_text, command=button_command)
        self.button.grid(row=grid_row, column=grid_column, sticky=grid_sticky, columnspan=grid_columnspan, rowspan=grid_rowspan)
        self.responsive_grid(grid_row, grid_column)

    def new_label(self, label_text, text_alignment="center", grid_row=0, grid_column=0, grid_sticky="NESW", grid_columnspan=1, grid_rowspan=1):
        self.label = ttk.Label(self.root_window, text=label_text, anchor=text_alignment)
        self.label.grid(row=grid_row, column=grid_column, sticky=grid_sticky, columnspan=grid_columnspan, rowspan=grid_rowspan)
        self.responsive_grid(grid_row, grid_column)

    def new_progress_bar(self, pg_length=250, pg_mode="determinate", grid_row=0, grid_column=0, grid_sticky="NESW", grid_columnspan=1, grid_rowspan=1):
        self.progress_bar = ttk.Progressbar(self.root_window, length=pg_length, mode=pg_mode)
        self.progress_bar.grid(row=grid_row, column=grid_column, sticky=grid_sticky, columnspan=grid_columnspan, rowspan=grid_rowspan)
        self.responsive_grid(grid_row, grid_column)

    def responsive_grid(self, row_responsive=0, column_responsive=0, row_weight_num=1, column_weight_num=1):
        self.root_window.grid_columnconfigure(column_responsive, weight=column_weight_num)
        self.root_window.grid_rowconfigure(row_responsive, weight=row_weight_num)

options_window = render_window(200, 250, "Options Window")

options_window.new_progress_bar()
options_window.progress_bar.start()
options_window.new_progress_bar(grid_column=1)
options_window.progress_bar.start()
options_window.new_label("Options Window\And other buttons...", grid_row=1, grid_columnspan=2)

options_window.root_window.mainloop()

我创建了一个允许使用tkinter相对容易地创建界面的系统。我在修改现有元素时遇到问题,如果创建多个实例,我似乎无法修改它们,我只能编辑最后创建的实例。当我说修改/编辑时,我说的是.config()。

所以每当我这样做: options_window.progress_bar.config(args_here),它只对最后创建的栏执行此操作。有没有办法指定我可以执行代码的哪个栏?

谢谢!

1 个答案:

答案 0 :(得分:2)

如果我理解正确的话......您是否可以将每个进度条设置为变量?即

pb1 = options_window.progress_bar
pb1.start()
pb1.conig('etc, etc')

很抱歉,如果我误解了你的问题!

PS - 很酷的主意!