简体   繁体   English

从 combobox 问题中获取元素 tkinter

[英]Gettting element from combobox problem in tkinter

I am trying to get the selected element from combobox in tkinter. I will use this selected element in my code.我正在尝试从 tkinter 中的 combobox 获取所选元素。我将在我的代码中使用这个所选元素。 There are some asked questions and posts in inte.net but they did not help me. inte.net 上有一些问题和帖子,但它们对我没有帮助。 So, I am sharing the related part of my code:所以,我正在分享我的代码的相关部分:

     self.dugme2 = Button(text="printer", command=self.ekle, fg="black", bg="green", font="bold")
     self.dugme2.place(relx=0.40,rely=0.65)

     self.ip_list2 = ttk.Combobox(width=15,state="readonly")
     self.ip_list2["values"] = ["eth0", "wan0","wan1"]
     self.ip_list2.place(relx=0.60, rely=0.08)
     self.ip_list2.set("Interfaces")
     self.selected_interf=self.ip_list2.get()

In the foregoing code, I tried to get the selected element using self.selected_interf=self.ip_list2.get() .在前面的代码中,我尝试使用self.selected_interf=self.ip_list2.get()获取选中的元素。 After that, I wanted to print it in the screen using button and following function by之后,我想使用按钮将其打印在屏幕上,然后跟随 function

   def ekle(self):
     self.etiket3 = Label(text=self.selected_interf, font="Times 11 bold", bg="grey")
     self.etiket3.place(rely=0.78, relx=0.39)

I want to print the selected element in the combobox on the label when i click on the button当我点击按钮时,我想在 label 上打印 combobox 中的选定元素

Can you help, please?你能帮忙吗?

You should call self.selected_interf=self.ip_list2.get() inside ekle() instead.您应该在ekle()中调用self.selected_interf=self.ip_list2.get() Also it is better to create label self.etiket3 once and update its text inside ekle() :此外,最好创建一次 label self.etiket3并在ekle()中更新其文本:

        self.dugme2 = Button(text="printer", command=self.ekle, fg="black", bg="green", font="bold")
        self.dugme2.place(relx=0.40, rely=0.65)

        self.ip_list2 = ttk.Combobox(width=15, state="readonly")
        self.ip_list2["values"] = ["eth0", "wan0", "wan1"]
        self.ip_list2.place(relx=0.60, rely=0.08)
        self.ip_list2.set("Interfaces")

        # create the label here instead of inside `ekle()`
        # initially it is hidden
        self.etiket3 = Label(font="Times 11 bold", bg="grey")
    def ekle(self):
        # get selection of combobox here
        self.selected_interf = self.ip_list2.get()
        # update label and show it
        self.etiket3.config(text=self.selected_interf)
        self.etiket3.place(rely=0.78, relx=0.39)

You need to modify your code as follows你需要修改你的代码如下

self.dugme2 = Button(text="printer", command=self.ekle, fg="black", bg="green", font="bold")
self.dugme2.place(relx=0.40,rely=0.65)

self.ip_list2 = ttk.Combobox(width=15,state="readonly")
self.ip_list2["values"] = ["eth0", "wan0","wan1"]
self.ip_list2.place(relx=0.60, rely=0.08)
self.ip_list2.set("Interfaces")

def ekle(self):
 self.selected_interf=self.ip_list2.get()
 self.etiket3 = Label(text=self.selected_interf, font="Times 11 bold", bg="grey")
 self.etiket3.place(rely=0.78, relx=0.39)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM