【问题标题】:TKinter problem with getting return values out of Entry from GUI Designer从 GUI 设计器的条目中获取返回值的 TKinter 问题
【发布时间】:2021-06-07 11:17:50
【问题描述】:

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

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

我遇到的问题是:按下按钮后,我的 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.x user-interface tkinter return-value


    【解决方案1】:

    在创建小部件时使用 self 使小部件在整个 App 类中可用。

    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()
    

    【讨论】:

      猜你喜欢
      • 2021-06-29
      • 1970-01-01
      • 1970-01-01
      • 2013-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多