1

我对 Python 很陌生。我可以让事情正常进行,但我从来不确定我是否真的在使用最佳实践,所以如果我没有正确解释事情,或者我的代码不是使用最佳实践编写的,请原谅我。我在做这个工作!

我试图弄清楚如何使用 tkinter 的 widget.config() 来修改由相同方法创建的单独小部件?例如,在下面的代码中,如何使用 widget.config() 修改结果部分 2 r2 中的文本?

from tkinter import *
from tkinter.ttk import *

class App():
    def __init__(self, master):
        self.master = master
        self.master.title('Question')
        self.create_main_frame()  

    def create_main_frame(self):
        self.main_f = Frame(self.master)
        nb = Notebook(padding=5)
        nb.pack(anchor='w')
        self.tab1(nb)

    def tab1(self, nb):
        tab1_frame = Frame(nb)
        nb.add(tab1_frame, text='Tab 1', underline=0)

        prod_lab = Label(tab1_frame, text='Entry:')
        prod_lab.grid(row=1, column=0, padx=(10,0), pady=(10,5), sticky='e')

        self.product_e = Entry(tab1_frame, width=40)
        self.product_e.grid(row=1, column=1, padx=(0,10), pady=(10,5))

        self.button1 = Button(tab1_frame,
                               text="Run",
                               command=self.run1
                               )
        self.button1.grid(row=2, column=1, padx=(0,10), pady=(0,10), sticky='ne')


        self.results1 = self.create_result(tab1_frame, 'Results Section 1')
        self.results2 = self.create_result(tab1_frame, 'Results Section 2')
        self.results3 = self.create_result(tab1_frame, 'Results Section 3')

    def create_result(self, frame, name):
        lab_frame = LabelFrame(master=frame, text=name)
        lab_frame.grid(sticky='we', padx=10, pady=10, columnspan=10)

        l1 = Label(master=lab_frame, text='Result 1:')
        l1.grid(sticky='e', row=0, column=0, pady=2)

        l2 = Label(master=lab_frame, text='Result 2:')
        l2.grid(sticky='e', row=1, column=0, pady=2)

        l3 = Label(master=lab_frame, text='Result 3:')
        l3.grid(sticky='e', row=2, column=0, pady=2)

        r1 = Label(master=lab_frame, text='')
        r1.grid(sticky='w', row=0, column=1, pady=2)

        r2 = Label(master=lab_frame, text='')
        r2.grid(sticky='w', row=1, column=1, pady=2)

        r3 = Label(master=lab_frame, text='')
        r3.grid(sticky='w', row=2, column=1, pady=2)

    def run1(self):
        #This is the method that will be used to find the results I need
        #For the sake of this question, let's pretend I want to put whatever text is in Entry 1, into results section 2 result 2
        print (self.product_e.get())
        print ('How do I use "widget.config()" to modify the 3 results sections separately???')

master = Tk()
app = App(master)
master.mainloop()

非常感谢您的帮助!

4

1 回答 1

0

我在 Tkinter 不好,所以我会改create_result()到 class Results

from Tkinter import *
from ttk import *

class Results():

    def __init__(self, frame, name):
        self.lab_frame = LabelFrame(master=frame, text=name)
        self.lab_frame.grid(sticky='we', padx=10, pady=10, columnspan=10)

        self.l1 = Label(master=self.lab_frame, text='Result 1:')
        self.l1.grid(sticky='e', row=0, column=0, pady=2)

        self.l2 = Label(master=self.lab_frame, text='Result 2:')
        self.l2.grid(sticky='e', row=1, column=0, pady=2)

        self.l3 = Label(master=self.lab_frame, text='Result 3:')
        self.l3.grid(sticky='e', row=2, column=0, pady=2)

        self.r1 = Label(master=self.lab_frame, text='')
        self.r1.grid(sticky='w', row=0, column=1, pady=2)

        self.r2 = Label(master=self.lab_frame, text='')
        self.r2.grid(sticky='w', row=1, column=1, pady=2)

        self.r3 = Label(master=self.lab_frame, text='')
        self.r3.grid(sticky='w', row=2, column=1, pady=2)

class App():
    def __init__(self, master):
        self.master = master
        self.master.title('Question')
        self.create_main_frame()  

    def create_main_frame(self):
        self.main_f = Frame(self.master)
        nb = Notebook(padding=5)
        nb.pack(anchor='w')
        self.tab1(nb)

    def tab1(self, nb):
        tab1_frame = Frame(nb)
        nb.add(tab1_frame, text='Tab 1', underline=0)

        prod_lab = Label(tab1_frame, text='Entry:')
        prod_lab.grid(row=1, column=0, padx=(10,0), pady=(10,5), sticky='e')

        self.product_e = Entry(tab1_frame, width=40)
        self.product_e.grid(row=1, column=1, padx=(0,10), pady=(10,5))

        self.button1 = Button(tab1_frame,
                               text="Run",
                               command=self.run1
                               )
        self.button1.grid(row=2, column=1, padx=(0,10), pady=(0,10), sticky='ne')


        self.results1 = Results(tab1_frame, 'Results Section 1')
        self.results2 = Results(tab1_frame, 'Results Section 2')
        self.results3 = Results(tab1_frame, 'Results Section 3')


    def run1(self):
        #This is the method that will be used to find the results I need
        #For the sake of this question, let's pretend I want to put whatever text is in Entry 1, into results section 2 result 2
        print (self.product_e.get())
        print ('How do I use "widget.config()" to modify the 3 results sections separately???')
        self.results1.r2.config(text='1111111111')
        self.results2.r2.config(text='xxxxxxxxxx')
        self.results3.r2.config(text='----------')

master = Tk()
app = App(master)
master.mainloop()
于 2013-11-09T01:15:21.363 回答