python-3.x - 从 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()
解决方案
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()
推荐阅读
- postgresql - 无法从 ResultSet 中检索 getGeneratedKey,在之前在 postgresql 中进行的插入中
- jquery - Laravel 4.2 按索引合并两个 Eloquent 对象
- django - Django modelform 小部件属性被替换或忽略
- python - 如何以默认数据格式从 tsv 文件中读取数据,这些数据格式混合并随机排列在列和行中?
- time-complexity - 为什么哈希表插入时间复杂度最坏情况不是N log N
- python - 非规范化公司名称的 DataFrame [第 2 部分]
- node.js - 为什么 DEBUG 没有登录控制台?
- ruby-on-rails - Rails 5 hasMany through 没有过滤
- unity3d - 在托管 CloudAnchor 后使用正确位置/旋转的 AugmentedImages
- laravel - Auth::check 在我设置警卫时返回 null