【发布时间】:2016-10-15 14:40:38
【问题描述】:
我对 python 很陌生,我刚开始使用 Tkinter。我正在尝试制作一些自我练习文件。到目前为止一切顺利,但我遇到了一个问题(我将发布整个代码,之后我将继续解决问题,以便您可以看到我想要做什么以及我不明白该怎么做)。
#!/usr/bin/python
from tkinter import *
from PIL import Image, ImageTk
import subprocess
class Window(Frame):
def __init__(self, master = None):
Frame.__init__(self, master)
self.master = master
self.init_window()
def init_window(self):
self.master.title("ez-Installer")
self.pack(fill=BOTH, expand=1)
updateButton = Button(self, text="Update", command=self.system_update)
updateButton.place(x=50, y=50)
syncButton = Button(self, text="Sync packages", command=self.system_sync)
syncButton.place(x=150, y=50)
cmd1 = StringVar()
mEntry = Entry(self,textvariable=cmd1).pack()
installButton = Button(self, text="Install", command=self.system_install)
installButton.place(x=50, y=150)
def system_install(self):
package = cmd1.get()
install = "sudo pacman -S {} --noconfirm".format(package)
subprocess.call([install], shell=True)
def system_exit(self):
exit()
def system_update(self):
subprocess.call(["sudo pacman -Su --noconfirm"], shell=True)
def system_sync(self):
subprocess.call(["sudo pacman -Syy --noconfirm"], shell=True)
root = Tk()
root.geometry("400x300")
app = Window(root)
root.mainloop()
错误是在按下“安装”按钮时。 “cmd1 未定义”。
def system_install(self):
package = cmd1.get()
install = "sudo pacman -S {} --noconfirm".format(package)
subprocess.call([install], shell=True)
如您所见,我希望它从我在此处添加的搜索框条目中获取文本:
cmd1 = StringVar()
mEntry = Entry(self,textvariable=cmd1).pack()
installButton = Button(self, text="Install", command=self.system_install)
installButton.place(x=50, y=150)
我知道我的条目在 def init_window(self): 下,但是如何从那里获取 cmd1 的值?是否可以?如果不是,或者如果太麻烦,类似的替代方案是什么?
【问题讨论】:
标签: python tkinter box tkinter-entry