【问题标题】:Value from new window, button, and script to main script/window python+tkinter从新窗口、按钮和脚本到主脚本/窗口 python+tkinter 的值
【发布时间】:2021-02-23 09:24:50
【问题描述】:

我有一个主脚本。当您按下按钮(在 tkinter 中)时,您将打开一个带有新窗口和新按钮的类。 当您单击新按钮(在新窗口和不同文件中)时,主窗口中的文本应该会更新。

我有以下几点:

主脚本

from tkinter import *
from kandit import Kandit

root=Tk()

def hoop():
    s=Kandit()
    label.configure(text=s)

button=Button(root, text="ok", command=hoop)
button.grid(row=0,column=0)

label=Label(root, text="nog niet dus")
label.grid(row=1, column=0)

子脚本

class Kandit:
  def __init__(self):
  self.Master=Toplevel()
  self.Button=Button(self.Master, text="check", command=self.Return())
  self.Button.grid(row=0,column=0)
  self.Master.mainloop()
def Return(self):
  self.Keuze="nothing"
  return self.Keuze #, self.Master.destroy()

除了销毁之外,它一直有效,直到我按下“检查”按钮。 比什么都没有发生。

【问题讨论】:

  • 首先你写了self.Button = Button(..., command=self.Return()),我想你想写self.Button = Button(..., command=self.Return)

标签: python function tkinter button


【解决方案1】:

试试这个:

import tkinter as tk


class Kandit:
    def __init__(self):
        # Set the default value for keuze:
        self.keuze = None
        self.master = tk.Toplevel()
        # If the user presses the "X" in the window toolbar call `_return`
        self.master.protocol("WM_DELETE_WINDOW", self.destroy)
        # When the button is pressed call `_return`
        self.button = tk.Button(self.master, text="check", command=self._return)
        self.button.grid(row=0, column=0)
        # Start the mainloop. Later we will stop the mainloop
        # Note it waits until the button is pressed/window closed
        self.master.mainloop()
        # Here we can garantee that `_return` was called
        # and `self.keuze` has set to the value we need.

    def _return(self):
        # Set the result in a variable
        self.keuze = "nothing"
        self.destroy()

    def destroy(self):
        # Stop the mainloop so that the program can continue
        self.master.quit()
        # Remove the window from the screen
        self.master.destroy()


def hoop():
    # Create the window and wait until the button is pressed/window closed
    new_window = Kandit()
    # Get the result from the class
    new_text = new_window.keuze
    #if 
    # Set the label with the result
    label.configure(text=new_text)


root = tk.Tk()

button = tk.Button(root, text="ok", command=hoop)
button.grid(row=0, column=0)

label = tk.Label(root, text="nog niet dus")
label.grid(row=1, column=0)

root.mainloop()

您的问题是您无法从 __init__ 方法返回值。这就是为什么您将结果保存到变量并稍后检索它的原因

【讨论】:

  • 如果通过点击标题栏的关闭按钮关闭第二个窗口则不起作用。
  • @acw1668 感谢我改变了我的答案
  • 使用标题栏关闭按钮关闭第二个窗口后,您更改仍然不更新主窗口中的标签。您需要改用self.master.protocol("WM_DELETE_WINDOW", self._return)
  • 是的,这非常有效。 Thnx TheLizzard,至于解决方案,以及为什么它一开始就不起作用的解释。我也在向你学习符号方法。再次感谢
  • @acw1668 我认为摧毁窗户会阻止mainloop(我的坏应该已经测试过了)。我不想让它太复杂,但我想我必须改变那个协议。我认为这是因为 OP 使用了 Toplevel 而我通常使用 Tk
猜你喜欢
  • 1970-01-01
  • 2013-03-20
  • 2021-12-15
  • 2020-04-02
  • 2022-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多