简体   繁体   English

来自类外的 Tkinter 函数被立即调用

[英]Tkinter functions from outside of class being immediately called

I am having some issues getting these functions to work properly for my tkinter app.我在让这些功能为我的 tkinter 应用程序正常工作时遇到一些问题。 I have two files, one containing the main Window class and the other containing a function that I am trying to connect to a button command.我有两个文件,一个包含主 Window 类,另一个包含我试图连接到按钮命令的函数。 The issue is the button is in the main Window and the function I am associating with its "click" is not updating and just seems to execute the function once through.问题是按钮在主窗口中,我与其“点击”关联的功能没有更新,似乎只是执行了一次。

Here are my two files: main.py这是我的两个文件: main.py

import customtkinter as ctk
from size import *

class Window(ctk.CTk):
WIDTH = 700 
HEIGHT = 600

def __init__(self) -> None:
    super().__init__()
    
    self.geometry(f"{Window.WIDTH}x{Window.HEIGHT}")
    self.title("Test")

#Setup Frames-----#
        #Configure grid layout (2x1)
        self.grid_columnconfigure(1, weight=1)
        self.grid_rowconfigure(0, weight=1)
        #Configure left frame
        self.frame_left = ctk.CTkFrame(master=self,width=180,corner_radius=0)
        self.frame_left.grid(row=0, column=0, sticky="nswe", padx=10,pady=10)
        #Configure right frame
        self.frame_right = ctk.CTkFrame(master=self)
        self.frame_right.grid(row=0, column=1, sticky="nswe", padx=10, pady=10)
        #Far Left Frame
        self.frame_left.grid_rowconfigure(0, minsize=10)

#Labels-----#
        #Left Frame Labels
        size_label = ctk.CTkLabel(master=self.frame_left, text="Size Option:")
        monster_name_label = ctk.CTkLabel(master=self.frame_left, text="Monster Name:")
        #Right Frame Labels
        display_monster_name = ctk.CTkLabel(master=self.frame_right, text='')
        display_size = ctk.CTkLabel(master=self.frame_right, text='')
        
#Comboboxes-----#
        #Size
        size_combobox = ctk.CTkComboBox(master=self.frame_left, values=size_options_combobox)
        size_combobox.set("Random")

#Functions-----#
        #Size 
        size_command = add_size(size_combobox, display_size)

#Buttons-----#
        #Size
        add_size_btn = ctk.CTkButton(master=self.frame_left,command=size_command, text="+", width=30)
        
#Grid Layout-----#
#Left frame grid layout
        #Row 1 
        size_label.grid(row=1, column=0)
        size_combobox.grid(row=1, column=1)
        add_size_btn.grid(row=1, column=2, sticky = "W")

#Right frame grid layout
        #Row 1
        display_size.grid(row=1,column=1)


    

if __name__ == "__main__":
    window = Window()
    window.mainloop()

The other file I am importing from is size.py:我从中导入的另一个文件是 size.py:

from main import *
import customtkinter as ctk
import random

def add_size(size_combobox, display_size):
size_choice = StringVar()
size_choice = size_combobox.get() #Suspect this maybe the issue
random_size = random.choice(size_options_label)
if size_choice == "Random":
    display_size['text'] = random_size
else:
    display_size['text'] = size_choice

I am suspecting that the issue may lie with the .get() call off of the add_size function because if I run this in the main.py within the Window class, it works and updates the Labels value with whatever the combobox choice is.我怀疑问题可能出在 add_size 函数的 .get() 调用上,因为如果我在 Window 类的 main.py 中运行它,它会工作并使用组合框选项更新 Labels 值。

Here is a screen shot of what it looks like when it runs once through and since it is set to "Random" the if statement executes once through with that as its value and won't update after another button click.这是它运行一次时的屏幕截图,由于它设置为“随机”,因此 if 语句以它的值执行一次,并且不会在另一个按钮单击后更新。

尺寸错误

You're explicitly asking for add_size to be called in this line:您明确要求在此行中调用add_size

size_command = add_size(size_combobox, display_size)

You need to change it to use lambda or functools.partial:您需要将其更改为使用 lambda 或 functools.partial:

size_command = lambda: add_size(size_combobox, display_size)

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

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