如何根据组合框选择更改多个Entry小部件? [英] How to change multiple Entry widgets based on Combobox selection?

查看:61
本文介绍了如何根据组合框选择更改多个Entry小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Yesterday I had a problem with a Combobox selection which can be seen at the following link:

< a href = https://stackoverflow.com/questions/16925920/how-to-change-multiple-labels-based-on-combobox-select>>如何根据组合框选择更改多个标签?

很遗憾,我无法表达自己的想法,所以我的问题没有解决。无论如何,谢谢您的帮助。我的意思是,用以下代码更好地表达问题,问题在于它使用的是 Listbox 而不是 Combobox

Unfortunately I was not be able to express my idea, so my problem was not be solved. Anyway, thanks for your kind help. What I really mean is better expressed by the following code, the problem is that it is using a Listbox instead 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])

    def Listbox_Content(self):
        for dat in range(len(self.myData)):
            self.listboxData.insert(END, self.myData[dat][1])

        self.listboxData.selection_set(0)

    def stablishment(self):
        mainFrame = Frame(self.parent)
        mainFrame.pack(fill=BOTH, expand=YES)

        self.statusBar = Label(mainFrame, text="App",relief=SUNKEN, bd=1)
        self.statusBar.pack(side=BOTTOM, fill=X)

        fr_left = Frame(mainFrame, bd=10)
        fr_left.pack(fill=BOTH, expand=YES, side=LEFT)

        scroll = Scrollbar(fr_left, orient=VERTICAL)
        self.listboxData = Listbox(fr_left, width=30,yscrollcommand=scroll.set)

        self.listboxData.pack(fill=Y, side=LEFT)
        scroll.configure(command=self.listboxData.yview)
        scroll.pack(side=LEFT, fill=Y)

        fr_right = Frame(mainFrame, bd=10)
        fr_right.pack(fill=BOTH, expand=YES, side=RIGHT)

        fr_up = Frame(fr_right)
        fr_up.pack(side=TOP, expand=YES)

        Label(fr_up, text='List Number').grid(row=0, column=0, sticky=W)
        self.entNumber = Entry(fr_up)
        self.entNumber.grid(row=0, column=1)

        Label(fr_up, text='Name').grid(row=1, column=0, sticky=W)
        self.entName = Entry(fr_up)
        self.entName.grid(row=1, column=1)

        Label(fr_up, text='City').grid(row=2, column=0, sticky=W)
        self.entCity = Entry(fr_up)
        self.entCity.grid(row=2, column=1)

        Label(fr_up, text='No. Tel').grid(row=3, column=0, sticky=W)
        self.entTel = Entry(fr_up)
        self.entTel.grid(row=3, column=1)

        Label(fr_up, text='Address').grid(row=4, column=0, sticky=W)
        self.entAddress = Entry(fr_up)
        self.entAddress.grid(row=4, column=1)

    def Closes(self, event=None):
        self.parent.destroy()

if __name__ == '__main__':
    app = MyListbox(root, "Main Window") 
    root.mainloop()


推荐答案

以下是A. Rodas的 answer 适应(或多或少)您之前提出的问题的代码中表达的想法:

Here's the code in the A. Rodas's answer to your earlier question adapted (more-or-less) to the idea expressed in code you posted with this question:

from Tkinter import *
import ttk

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", "7 SD"],
            ["3", "Steven Mc Fly", "Rome", "0341-123456", "8 SD"],
            ["4", "Joao Pontes", "Rio", "0341-234567", "9 SD"],
            ["5", "Kenji S.", "Tokyo", "0341-213212", "10 SD"])

        self.establishment()

    def combobox_handler(self, event):
        current = self.combobox.current()
        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[current][0])
        self.entName.insert(END, self.myData[current][1])
        self.entCity.insert(END, self.myData[current][2])
        self.entTel.insert(END, self.myData[current][3])
        self.entAddress.insert(END, self.myData[current][4])

    def establishment(self):
        mainFrame = Frame(self.parent)
        mainFrame.pack(fill=BOTH, expand=YES)

        self.statusBar = Label(mainFrame, text="App",relief=SUNKEN, bd=1)
        self.statusBar.pack(side=BOTTOM, fill=X)

        fr_left = Frame(mainFrame, bd=10)
        fr_left.pack(fill=BOTH, expand=YES, side=LEFT)

        names = [person[1] for person in self.myData]
        self.combobox = ttk.Combobox(fr_left, values=names)
        self.combobox.bind('<<ComboboxSelected>>', self.combobox_handler)
        self.combobox.pack()

        fr_right = Frame(mainFrame, bd=10)
        fr_right.pack(fill=BOTH, expand=YES, side=RIGHT)

        fr_up = Frame(fr_right)
        fr_up.pack(side=TOP, expand=YES)

        Label(fr_up, text='List Number').grid(row=0, column=0, sticky=W)
        self.entNumber = Entry(fr_up)
        self.entNumber.grid(row=0, column=1)

        Label(fr_up, text='Name').grid(row=1, column=0, sticky=W)
        self.entName = Entry(fr_up)
        self.entName.grid(row=1, column=1)

        Label(fr_up, text='City').grid(row=2, column=0, sticky=W)
        self.entCity = Entry(fr_up)
        self.entCity.grid(row=2, column=1)

        Label(fr_up, text='No. Tel').grid(row=3, column=0, sticky=W)
        self.entTel = Entry(fr_up)
        self.entTel.grid(row=3, column=1)

        Label(fr_up, text='Address').grid(row=4, column=0, sticky=W)
        self.entAddress = Entry(fr_up)
        self.entAddress.grid(row=4, column=1)

    def closes(self, event=None):
        self.parent.destroy()

if __name__ == '__main__':
    app = MyListbox(root, "Main Window")
    root.mainloop()

这篇关于如何根据组合框选择更改多个Entry小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆