程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了从新窗口、按钮和脚本到主脚本/窗口的值 python+tkinter大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决从新窗口、按钮和脚本到主脚本/窗口的值 python+tkinter?

开发过程中遇到从新窗口、按钮和脚本到主脚本/窗口的值 python+tkinter的问题如何解决?下面主要结合日常开发的经验,给出你关于从新窗口、按钮和脚本到主脚本/窗口的值 python+tkinter的解决方法建议,希望对你解决从新窗口、按钮和脚本到主脚本/窗口的值 python+tkinter有所启发或帮助;

我有一个主脚本。当您按下按钮(在 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()

除了销毁它一直工作到我按下“检查”按钮的那一刻。 比什么都没有发生。

解决方法

试试这个:

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__ 方法返回值。这就是为什么您要将结果保存到变量并稍后检索它的原因

大佬总结

以上是大佬教程为你收集整理的从新窗口、按钮和脚本到主脚本/窗口的值 python+tkinter全部内容,希望文章能够帮你解决从新窗口、按钮和脚本到主脚本/窗口的值 python+tkinter所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: