如何根据组合框选择更改多个Entry小部件?

昨天我在Combobox选择时遇到了问题,可以在以下链接中看到:


如何根据组合框选择更改多个标签?


不幸的是,我无法表达自己的想法,因此我的问题没有得到解决。无论如何,谢谢您的帮助。我的意思是,用下面的代码更好地表达问题,问题在于它使用了aListbox而不是a Combobox。


这是代码:


from Tkinter import *

root = Tk()

root.minsize(500,300)

root.maxsize(550,310)


class MyListbox:

    def __init__(self, parent, title):

        self.parent = parent

        self.parent.title(title)

        self.parent.protocol("WM_DELETE_WINDOW", self.Closes)


        self.myData= (

            ["1", "Jhon Doe", "Madrid", "0341-672541", "6 SD"],

            ["2", "Mike Grant", "Barcelona", "0341-435271", "6 SD"],

            ["3", "Steven Mc Fly", "Rome", "0341-123456", "6 SD"],

            ["4", "Joao Pontes", "Rio", "0341-234567", "6 SD"],

            ["5", "Kenji S.", "Tokyo", "0341-213212", "6 SD"])


        self.stablishment()

        self.SeT()

        self.Listbox_Content()

        self.Data_Content()


        self.listboxData.focus_set()


    def SeT(self):

        self.listboxData.bind('<ButtonRelease-1>', self.click)

        self.listboxData.bind('<KeyRelease>', self.click)


    def click(self, event=None):

        self.Data_Content()


    def Data_Content(self):

        index = self.listboxData.curselection()

        code = int(index[0])


        self.entNumber.delete(0, END)

        self.entName.delete(0, END)

        self.entCity.delete(0, END)

        self.entTel.delete(0, END)

        self.entAddress.delete(0, END)


        self.entNumber.insert(END, self.myData[code][0])

        self.entName.insert(END, self.myData[code][1])

        self.entCity.insert(END, self.myData[code][2])

        self.entTel.insert(END, self.myData[code][3])

        self.entAddress.insert(END, self.myData[code][4])


胡子哥哥
浏览 200回答 1
1回答

慕侠2389804

该代码适应(或多或少)适应您在此问题中发布的代码中表达的思想:from Tkinter import *import ttkroot = Tk()root.minsize(500,300)root.maxsize(550,310)class MyListbox:&nbsp; &nbsp; def __init__(self, parent, title):&nbsp; &nbsp; &nbsp; &nbsp; self.parent = parent&nbsp; &nbsp; &nbsp; &nbsp; self.parent.title(title)&nbsp; &nbsp; &nbsp; &nbsp; self.parent.protocol("WM_DELETE_WINDOW", self.closes)&nbsp; &nbsp; &nbsp; &nbsp; self.myData= (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ["1", "Jhon Doe", "Madrid", "0341-672541", "6 SD"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ["2", "Mike Grant", "Barcelona", "0341-435271", "7 SD"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ["3", "Steven Mc Fly", "Rome", "0341-123456", "8 SD"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ["4", "Joao Pontes", "Rio", "0341-234567", "9 SD"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ["5", "Kenji S.", "Tokyo", "0341-213212", "10 SD"])&nbsp; &nbsp; &nbsp; &nbsp; self.establishment()&nbsp; &nbsp; def combobox_handler(self, event):&nbsp; &nbsp; &nbsp; &nbsp; current = self.combobox.current()&nbsp; &nbsp; &nbsp; &nbsp; self.entNumber.delete(0, END)&nbsp; &nbsp; &nbsp; &nbsp; self.entName.delete(0, END)&nbsp; &nbsp; &nbsp; &nbsp; self.entCity.delete(0, END)&nbsp; &nbsp; &nbsp; &nbsp; self.entTel.delete(0, END)&nbsp; &nbsp; &nbsp; &nbsp; self.entAddress.delete(0, END)&nbsp; &nbsp; &nbsp; &nbsp; self.entNumber.insert(END, self.myData[current][0])&nbsp; &nbsp; &nbsp; &nbsp; self.entName.insert(END, self.myData[current][1])&nbsp; &nbsp; &nbsp; &nbsp; self.entCity.insert(END, self.myData[current][2])&nbsp; &nbsp; &nbsp; &nbsp; self.entTel.insert(END, self.myData[current][3])&nbsp; &nbsp; &nbsp; &nbsp; self.entAddress.insert(END, self.myData[current][4])&nbsp; &nbsp; def establishment(self):&nbsp; &nbsp; &nbsp; &nbsp; mainFrame = Frame(self.parent)&nbsp; &nbsp; &nbsp; &nbsp; mainFrame.pack(fill=BOTH, expand=YES)&nbsp; &nbsp; &nbsp; &nbsp; self.statusBar = Label(mainFrame, text="App",relief=SUNKEN, bd=1)&nbsp; &nbsp; &nbsp; &nbsp; self.statusBar.pack(side=BOTTOM, fill=X)&nbsp; &nbsp; &nbsp; &nbsp; fr_left = Frame(mainFrame, bd=10)&nbsp; &nbsp; &nbsp; &nbsp; fr_left.pack(fill=BOTH, expand=YES, side=LEFT)&nbsp; &nbsp; &nbsp; &nbsp; names = [person[1] for person in self.myData]&nbsp; &nbsp; &nbsp; &nbsp; self.combobox = ttk.Combobox(fr_left, values=names)&nbsp; &nbsp; &nbsp; &nbsp; self.combobox.bind('<<ComboboxSelected>>', self.combobox_handler)&nbsp; &nbsp; &nbsp; &nbsp; self.combobox.pack()&nbsp; &nbsp; &nbsp; &nbsp; fr_right = Frame(mainFrame, bd=10)&nbsp; &nbsp; &nbsp; &nbsp; fr_right.pack(fill=BOTH, expand=YES, side=RIGHT)&nbsp; &nbsp; &nbsp; &nbsp; fr_up = Frame(fr_right)&nbsp; &nbsp; &nbsp; &nbsp; fr_up.pack(side=TOP, expand=YES)&nbsp; &nbsp; &nbsp; &nbsp; Label(fr_up, text='List Number').grid(row=0, column=0, sticky=W)&nbsp; &nbsp; &nbsp; &nbsp; self.entNumber = Entry(fr_up)&nbsp; &nbsp; &nbsp; &nbsp; self.entNumber.grid(row=0, column=1)&nbsp; &nbsp; &nbsp; &nbsp; Label(fr_up, text='Name').grid(row=1, column=0, sticky=W)&nbsp; &nbsp; &nbsp; &nbsp; self.entName = Entry(fr_up)&nbsp; &nbsp; &nbsp; &nbsp; self.entName.grid(row=1, column=1)&nbsp; &nbsp; &nbsp; &nbsp; Label(fr_up, text='City').grid(row=2, column=0, sticky=W)&nbsp; &nbsp; &nbsp; &nbsp; self.entCity = Entry(fr_up)&nbsp; &nbsp; &nbsp; &nbsp; self.entCity.grid(row=2, column=1)&nbsp; &nbsp; &nbsp; &nbsp; Label(fr_up, text='No. Tel').grid(row=3, column=0, sticky=W)&nbsp; &nbsp; &nbsp; &nbsp; self.entTel = Entry(fr_up)&nbsp; &nbsp; &nbsp; &nbsp; self.entTel.grid(row=3, column=1)&nbsp; &nbsp; &nbsp; &nbsp; Label(fr_up, text='Address').grid(row=4, column=0, sticky=W)&nbsp; &nbsp; &nbsp; &nbsp; self.entAddress = Entry(fr_up)&nbsp; &nbsp; &nbsp; &nbsp; self.entAddress.grid(row=4, column=1)&nbsp; &nbsp; def closes(self, event=None):&nbsp; &nbsp; &nbsp; &nbsp; self.parent.destroy()if __name__ == '__main__':&nbsp; &nbsp; app = MyListbox(root, "Main Window")&nbsp; &nbsp; root.mainloop()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python