Python - Tkinter - 如何从下拉选项中获取值并传递给另一个函数

尚基

请建议如何在下面的代码片段中的另一个函数(sample())中使用选定的下拉值。

我能够创建两个函数 fun() 和 fun2() ,它们返回第一个下拉值和第二个下拉值,但我无法将选定的下拉值作为参数传递。

import sys
import tkinter.messagebox as box
from tkinter.filedialog import asksaveasfile
if sys.version_info[0] >= 3:
    import tkinter as tk
else:
    import Tkinter as tk


class App(tk.Frame):

    def __init__(self, master):
        tk.Frame.__init__(self, master)

        self.dict = {'Asia': ['Japan', 'China', 'Malaysia'],
                     'Europe': ['Germany', 'France', 'Switzerland']}

        self.variable_a = tk.StringVar(self)
        self.variable_b = tk.StringVar(self)

        self.variable_b.trace('w', self.fun2)
        self.variable_a.trace('w', self.update_options)


        self.optionmenu_a = tk.OptionMenu(self, self.variable_a, *self.dict.keys(), command=self.fun)
        self.optionmenu_b = tk.OptionMenu(self, self.variable_b, '')


        username = self.fun()
        password = self.fun2()


        self.button = tk.Button(self, text="Login", command=lambda : sample(username=username, password=password))


        self.variable_a.set('Asia')

        self.optionmenu_a.pack()
        self.optionmenu_b.pack()
        self.button.pack()
        self.pack()

    def fun(self,*args):
        return self.variable_a.get()

    def fun2(self, *args):
        return self.variable_b.get()


    def update_options(self, *args):
        countries = self.dict[self.variable_a.get()]
        self.variable_b.set(countries[0])

        menu = self.optionmenu_b['menu']
        menu.delete(0, 'end')

        for country in countries:
            menu.add_command(label=country, command=lambda nation=country: self.variable_b.set(nation))


def sample(username, password):
    box.showinfo('info', 'Enter Credentials')


if __name__ == "__main__":
    root = tk.Tk()
    username = "root"
    password = "admin"
    app = App(root)
    app.mainloop()
刀锋战士

就在第一个下拉列表调用它的命令self.funan之前self.variable_b.trace('w', self.fun2),下拉列表 2 将比下拉列表 1 更快地更改其值,这可以通过 fun/fun2 中的打印来确认:

def fun(self,*args):
    print("value 1dd: " + self.variable_a.get())
    return self.variable_a.get()

def fun2(self, *args):
    print("value 2dd: " + self.variable_b.get())
    return self.variable_b.get()

我不会使用 ,optionmenu因为它无法接收焦点,您可以使用组合框代替,如果问题是Please suggest how to use the selected drop down value in another function然后看看这个例子:

import sys
import tkinter.messagebox as box
from tkinter.filedialog import asksaveasfile
if sys.version_info[0] >= 3:
    import tkinter as tk
    import tkinter.ttk as ttk
else:
    import Tkinter as tk


class App(tk.Frame):

    def __init__(self, master):
        tk.Frame.__init__(self, master)

        self.dict = {'Asia': ['Japan', 'China', 'Malaysia'],
                     'Europe': ['Germany', 'France', 'Switzerland']}

        self.variable_a = tk.StringVar(self)
        self.variable_b = tk.StringVar(self)
        self.last_county = tk.StringVar(self)
        self.area = tk.StringVar(self)
        self.country = tk.StringVar(self)

        self.variable_b.trace('w', self.fun2)
        self.variable_a.trace('w', self.update_options)


        self.combobox_a = ttk.Combobox(self, values=list(self.dict.keys()), state='readonly')
        self.combobox_a.bind("<<ComboboxSelected>>", self.fun)
        self.combobox_a.current(0)
        self.combobox_b = ttk.Combobox(self, values=self.dict['Asia'], state='readonly')
        self.combobox_b.bind("<<ComboboxSelected>>", self.fun2)
        self.combobox_b.current(0)


        username = self.fun()
        password = self.fun2()


        self.button = tk.Button(self, text="Login", command=lambda : sample(username, password, self.area, self.country))


        # self.variable_a.set('Asia')

        self.combobox_a.pack()
        self.combobox_b.pack()
        self.button.pack()
        self.pack()

    def fun(self,*args):
        print("changed 1-st combobox value to: " + self.combobox_a.get())
        if self.last_county != self.combobox_a.get():
            self.combobox_b['values']=self.dict[self.combobox_a.get()]
            self.combobox_b.current(0)
        self.last_county = self.area = self.combobox_a.get()
        return self.variable_a.get()

    def fun2(self, *args):
        print("changed 2-nd combobox value to" + self.combobox_b.get())
        self.country = self.combobox_b.get()
        return self.variable_b.get()

    def update_options(self, *args):
        countries = self.dict[self.variable_a.get()]
        self.variable_b.set(countries[0])

        menu = self.combobox_b['menu']
        menu.delete(0, 'end')

        for country in countries:
            menu.add_command(label=country, command=lambda nation=country: self.variable_b.set(nation))


def sample(username, password, area, country):
    box.showinfo('info', 'Selected area: ' + area + '\nSelected country: ' + country + '\nEnter Credentials')

if __name__ == "__main__":
    root = tk.Tk()
    username = "root"
    password = "admin"
    app = App(root)
    app.mainloop()

我创建了两个变量self.areaself.country它们从fun()/fun2()函数中获取其值,并展示了如何在sample()函数中使用它们

更新

我不知道,但我猜你要创建这样的东西:

import sys
import tkinter.messagebox as box
from tkinter.filedialog import asksaveasfile
if sys.version_info[0] >= 3:
    import tkinter as tk
    import tkinter.ttk as ttk
else:
    import Tkinter as tk


class App(tk.Frame):

    def __init__(self, master):
        tk.Frame.__init__(self, master)
        self.dict = {'Asia': ['Japan', 'China', 'Malaysia'],
                     'Europe': ['Germany', 'France', 'Switzerland']}
        # Init labels
        self.label_a = tk.Label(self, text="User Name: ")
        self.label_b = tk.Label(self, text="Password: ")
        # Initialize entries
        self.entry_a = tk.Entry(self)
        self.entry_b = tk.Entry(self, show='*') # Make a password entry
        # Add clear on double-click
        self.entry_a.bind("<Double-1>", lambda cl: self.entry_a.delete(0, "end"))
        self.entry_b.bind("<Double-1>", lambda cl: self.entry_b.delete(0, "end"))
        # Set default text
        self.entry_a.insert("0", "BladeMight")
        self.entry_b.insert("0", "lalala7x256")
        # Initialize comboboxes
        self.combobox_a = ttk.Combobox(self, values=list(self.dict.keys()), state='readonly')
        self.combobox_b = ttk.Combobox(self, values=self.dict['Asia'], state='readonly')
        # Select 0 element
        self.combobox_a.current(0)
        self.combobox_b.current(0)
        # Add event to update variables on combobox's value change event
        self.combobox_a.bind("<<ComboboxSelected>>", lambda f1: self.fun())
        self.combobox_b.bind("<<ComboboxSelected>>", lambda f2: self.fun2())
        # Initialize variables
        self.area = self.combobox_a.get()
        self.last_area = self.country = self.combobox_b.get()
        self.username = self.password = tk.StringVar(self)
        # Intialize button with command to call sample with arguments
        self.button = tk.Button(self, text="Login", command=lambda: sample(self.area, self.country, self.entry_a.get(), self.entry_b.get()))
        # Place all controls to frame
        self.label_a.pack()
        self.entry_a.pack()
        self.label_b.pack()
        self.entry_b.pack()
        self.combobox_a.pack(pady=5)
        self.combobox_b.pack(pady=5)
        self.button.pack()
        self.pack()

    def fun(self):
        print("changed 1-st combobox value to: " + self.combobox_a.get())
        if self.last_area != self.combobox_a.get():
            self.combobox_b['values']=self.dict[self.combobox_a.get()]
            self.combobox_b.current(0)
            self.country = self.combobox_b.get()
        self.last_area = self.area = self.combobox_a.get()

    def fun2(self):
        print("changed 2-nd combobox value to: " + self.combobox_b.get())
        self.country = self.combobox_b.get()

def sample(area, country, username, password):
    box.showinfo('info', 'User Name: ' + username + '\nPassword: ' + password + '\n' + 'Selected area: ' + area + '\nSelected country: ' + country + '\n')

if __name__ == "__main__":
    root = tk.Tk()
    username = "root"
    password = "admin"
    app = App(root)
    app.mainloop()

正确的?

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python 3将一个函数传递给另一个函数

来自分类Dev

并行Python:将在另一个模块中编写的函数传递给“提交”

来自分类Dev

Python Tkinter:传递列表参数以填充另一个列表

来自分类Dev

Python MYSQL tupple问题。如何将参数传递给另一个类中执行查询的函数

来自分类Dev

Python Tkinter-如何从一个函数到另一个函数使用值?

来自分类Dev

如何将带有参数作为参数的函数传递给Python中的另一个函数?

来自分类Dev

Python和Tkinter,将列表信息从一个函数传递到另一个函数

来自分类Dev

Python中的Tkinter枚举

来自分类Dev

在python Tkinter中的另一个框架内

来自分类Dev

如何更改另一个类中单击的按钮的文本?(Python-tkinter)

来自分类Dev

无法在tkinter python中的另一个窗口中访问列表

来自分类Dev

Python,Tkinter中的Getters

来自分类Dev

如何使用python tkinter调用一个窗口到另一个窗口

来自分类Dev

如何从Tkinter中的一个类函数获取值

来自分类Dev

如何使用Tkinter从python中的另一个文件访问函数。我找不到针对自己特定问题的任何解决方案

来自分类Dev

如果我在一个函数中创建了小部件,如何使用Python Tkinter在另一个函数中访问它们?

来自分类Dev

如何在python中将一个函数传递给另一个函数

来自分类Dev

Python 3将一个函数传递给另一个函数

来自分类Dev

如何从另一个文件函数获取值?Python

来自分类Dev

使用tkinter将输入字符串传递给另一个函数

来自分类Dev

Python Tkinter-如何从一个函数到另一个函数使用值?

来自分类Dev

如何将在tkinter的输入框中输入的文本从一个函数传递给另一个函数?

来自分类Dev

从另一个函数 A 关闭 Tkinter GUI 并将 Tkinter 变量传递给函数 A

来自分类Dev

我如何在 python tkinter 中不点击按钮的情况下进入另一个页面

来自分类Dev

将可选函数(和可选参数)传递给 Python 中的另一个函数?

来自分类Dev

python Tkinter:如何在调用在不同线程中运行的另一个函数之前加载带有“正在进行的工作”的状态栏

来自分类Dev

如何从另一个类正确访问一个类的 StringVar() - Python - tkinter

来自分类Dev

Python - Tkinter - 从另一个存档引用时,动态检查按钮不会获取值

来自分类Dev

将函数参数作为参数传递给python中的另一个函数

Related 相关文章

  1. 1

    Python 3将一个函数传递给另一个函数

  2. 2

    并行Python:将在另一个模块中编写的函数传递给“提交”

  3. 3

    Python Tkinter:传递列表参数以填充另一个列表

  4. 4

    Python MYSQL tupple问题。如何将参数传递给另一个类中执行查询的函数

  5. 5

    Python Tkinter-如何从一个函数到另一个函数使用值?

  6. 6

    如何将带有参数作为参数的函数传递给Python中的另一个函数?

  7. 7

    Python和Tkinter,将列表信息从一个函数传递到另一个函数

  8. 8

    Python中的Tkinter枚举

  9. 9

    在python Tkinter中的另一个框架内

  10. 10

    如何更改另一个类中单击的按钮的文本?(Python-tkinter)

  11. 11

    无法在tkinter python中的另一个窗口中访问列表

  12. 12

    Python,Tkinter中的Getters

  13. 13

    如何使用python tkinter调用一个窗口到另一个窗口

  14. 14

    如何从Tkinter中的一个类函数获取值

  15. 15

    如何使用Tkinter从python中的另一个文件访问函数。我找不到针对自己特定问题的任何解决方案

  16. 16

    如果我在一个函数中创建了小部件,如何使用Python Tkinter在另一个函数中访问它们?

  17. 17

    如何在python中将一个函数传递给另一个函数

  18. 18

    Python 3将一个函数传递给另一个函数

  19. 19

    如何从另一个文件函数获取值?Python

  20. 20

    使用tkinter将输入字符串传递给另一个函数

  21. 21

    Python Tkinter-如何从一个函数到另一个函数使用值?

  22. 22

    如何将在tkinter的输入框中输入的文本从一个函数传递给另一个函数?

  23. 23

    从另一个函数 A 关闭 Tkinter GUI 并将 Tkinter 变量传递给函数 A

  24. 24

    我如何在 python tkinter 中不点击按钮的情况下进入另一个页面

  25. 25

    将可选函数(和可选参数)传递给 Python 中的另一个函数?

  26. 26

    python Tkinter:如何在调用在不同线程中运行的另一个函数之前加载带有“正在进行的工作”的状态栏

  27. 27

    如何从另一个类正确访问一个类的 StringVar() - Python - tkinter

  28. 28

    Python - Tkinter - 从另一个存档引用时,动态检查按钮不会获取值

  29. 29

    将函数参数作为参数传递给python中的另一个函数

热门标签

归档