【问题标题】:Tkinter - How to pass a variable using modules?Tkinter - 如何使用模块传递变量?
【发布时间】:2021-11-11 18:36:53
【问题描述】:

我需要从用户那里获取一个值,然后将该值传递给函数以获取具有该值的字符串。我在第一个文件中的代码如下所示(当然是简化版):

import tkinter as tk
from tkinter import ttk
import myfunction  # this is my module that has another function

class Interface(ttk.Frame):

    def __init__(self, container):
        super().__init__(container)

        self.user_price_minimum = tk.StringVar()

        minimum_price = ttk.Label(self, text="Minimum price is: ")
        minimum_price.grid(row=0, column=0, padx=5, pady=5)
        minimum_price_entry = ttk.Entry(self, width=15, textvariable = self.user_price_minimum)
        minimum_price_entry.grid(row=0, column=1)
        minimum_price_entry.focus()

        #############
        button = ttk.Button(self, text="Use price")

        button.grid(column=0, rows=2, columnspan=2, padx=5, pady=5)

root = tk.Tk()
root.geometry("450x250")
root.title("Looking for a flat")
root.columnconfigure(0, weight=1)

frame = Interface(root)
frame.pack()

root.mainloop()

我的另一个调用myfunction.py 的python 文件应该能够从用户那里获取minimum_price 并将其添加到字符串中。代码如下:

def minimum_price(self):
    price_min = self.user_price_minimum.get()
    price_min = int(price_min)

    print(f'Minimum price is: {price_min}')

所以我不确定如何将用户的minimum_price 值用于此函数。

【问题讨论】:

    标签: python class object tkinter


    【解决方案1】:

    最简单的就是这样做:

    button = ttk.Button(self, text="Use price", command=lambda: myfunction.minimum_price(self))
    

    但是您也可以将其定义为类本身的方法:

    class Interface(ttk.Frame):
        
        def __init__(self, container):
            super().__init__(container)
        
            self.user_price_minimum = tk.StringVar()
                
            minimum_price = ttk.Label(self, text="Minimum price is: ")
            minimum_price.grid(row=0, column=0, padx=5, pady=5)
            minimum_price_entry = ttk.Entry(self, width=15, textvariable = self.user_price_minimum)
            minimum_price_entry.grid(row=0, column=1)
            minimum_price_entry.focus()
            
            #############
            button = ttk.Button(self, text="Use price", command=self.print_price)
            button.grid(column=0, rows=2, columnspan=2, padx=5, pady=5)  
            
        def print_price(self):
            price = self.user_price_minimum.get()
            print(f'Minimum price: {price}')
    

    我个人的偏好(在这种情况下使用另一个文件时(我可能更愿意像上面那样定义它))是:

    # in the other file
    def minimum_price(value):
        print(f'Minimum price: {value}')
    
    # inside the class
    button = ttk.Button(self, text="Use price", command=lambda: myfunction.minimum_price(self.user_price_minimum.get()))
    

    同样在这种情况下,您不一定需要StringVar,您也可以简单地使用以下方法获取值:

    # assignment
    self.minimum_price_entry = ttk.Entry(self, width=15)
    
    # get value (probably in some function call, basically the same way as with the `StringVar` except less code)
    self.minimum_price_entry.get()
    

    【讨论】:

    • 谢谢伙计!它适用于 lambda。感谢您提供第二个选项,但我想将我的函数保存在另一个 python 文件中
    • @Tmiskiewicz btw 在当前函数中你不需要将值转换为int,你只是打印它(如果你以后添加这样的计算,你可能需要整数值(在这种情况下,由于它是用户输入,您可能需要添加一些 try/exceptif statements 以检查或捕获如果值无法转换为整数的错误))
    • command=print_price 应该是 command=self.print_price
    猜你喜欢
    • 2014-06-12
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 2019-12-10
    • 2017-05-26
    • 2016-12-28
    • 2015-08-10
    • 1970-01-01
    相关资源
    最近更新 更多