首页 > 解决方案 > 从 GUI 设计器的条目中获取返回值的 TKinter 问题

问题描述

首先:我是一个绝对的菜鸟。我目前正在尝试用 tkinter 编写一个简单的 GUI,但我失败了。下面的大部分代码是由 GUI 设计人员而不是我编写的。如果我自己做,也许我会快得多。大多数教程都采用其他方式或使用 Pyhton 2,所以我只是不知道如何修复这个该死的东西。

程序应该做什么:这个东西旨在成为...批处理脚本的 GUI。该脚本采用源路径和目标路径,过滤某些文件扩展名并将它们从 A 复制到 B,同时保留其他任何内容。这个 GUI 应该向它提供数据。输入源,输入目标按开始,批处理脚本开始艰难地聚会。

我遇到的问题是:按下按钮后,我的 Python GUI 甚至无法回显变量。我完全知道问题出在哪里,但经过多次尝试后,我就是不知道如何管理它。它是 GiveTarget.get() 和 GiveSource.get() 以及 StartButton_command 函数。(注释掉了)

    import tkinter as tk
import tkinter.font as tkFont
import subprocess
import os

class App:
    def __init__(self, root):
        
        

        GiveSource=tk.Entry(root)
        GiveSource["borderwidth"] = "1px"
        ft = tkFont.Font(family='Times',size=10)
        GiveSource["font"] = ft
        GiveSource["fg"] = "#333333"
        GiveSource["justify"] = "left"
        GiveSource.place(x=110,y=50,width=328,height=30)
        global x
        x = GiveSource.get() #####This is wrong!#####
        
        GiveTarget=tk.Entry(root)
        GiveTarget["borderwidth"] = "1px"
        ft = tkFont.Font(family='Times',size=10)
        GiveTarget["font"] = ft
        GiveTarget["fg"] = "#333333"
        GiveTarget["justify"] = "left"
        GiveTarget.place(x=110,y=120,width=327,height=30)
        global y
        y = GiveTarget.get() #####This is also wrong!#####

        
        StartButton=tk.Button(root)
        StartButton["activebackground"] = "#90ee90"
        StartButton["activeforeground"] = "#ffffff"
        StartButton["bg"] = "#5fb878"
        ft = tkFont.Font(family='Times',size=10)
        StartButton["font"] = ft
        StartButton["fg"] = "#000000"
        StartButton["justify"] = "center"
        StartButton["text"] = "Start"
        StartButton.place(x=110,y=180,width=323,height=44)
        StartButton["command"] = self.StartButton_command

       



   

    def StartButton_command(self): #"Start" Button
    
        print("Start")
        
        print ("Source=" + x)
        print ("Target=" + y)
        
        print ("called HDD Copy")
        #subprocess.Popen([r'HDD_Kopierer.bat'])


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

标签: python-3.xuser-interfacetkinterreturn-value

解决方案


Make the widgets available throughout the App class by using self when you create them.

import tkinter as tk
import tkinter.font as tkFont
import subprocess
import os

class App:
    def __init__(self, root):
        self.GiveSource=tk.Entry(root)
        self.GiveSource["borderwidth"] = "1px"
        ft = tkFont.Font(family='Times',size=10)
        self.GiveSource["font"] = ft
        self.GiveSource["fg"] = "#333333"
        self.GiveSource["justify"] = "left"
        self.GiveSource.place(x=110,y=50,width=328,height=30)
  
        self.GiveTarget=tk.Entry(root)
        self.GiveTarget["borderwidth"] = "1px"
        ft = tkFont.Font(family='Times',size=10)
        self.GiveTarget["font"] = ft
        self.GiveTarget["fg"] = "#333333"
        self.GiveTarget["justify"] = "left"
        self.GiveTarget.place(x=110,y=120,width=327,height=30)

        
        StartButton=tk.Button(root)
        StartButton["activebackground"] = "#90ee90"
        StartButton["activeforeground"] = "#ffffff"
        StartButton["bg"] = "#5fb878"
        ft = tkFont.Font(family='Times',size=10)
        StartButton["font"] = ft
        StartButton["fg"] = "#000000"
        StartButton["justify"] = "center"
        StartButton["text"] = "Start"
        StartButton.place(x=110,y=180,width=323,height=44)
        StartButton["command"] = self.StartButton_command


    def StartButton_command(self): #"Start" Button
    

        print("Start")
        
        print ("Source=" + self.GiveSource.get())
        print ("Target=" + self.GiveTarget.get())
        
        print ("called HDD Copy")
        #subprocess.Popen([r'HDD_Kopierer.bat'])


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

推荐阅读