【发布时间】:2015-05-26 14:58:00
【问题描述】:
在使用 Tkinter 时大吃一惊——我想做的是一个运行系统调用函数的小型 GUI。
我希望能够使用 GUI 将 v1、v2、v3 设置为字符串项 - 它们用于“命令”功能。
def system_call( step_name, cmd ):
try:
subprocess.check_call(cmd, shell=True)
except subprocess.CalledProcessError as scall:
print "Script failed at %s stage - exit code was %s" % (step_name, scall.returncode)
exit()
def command(v1, v2, v3):
# Commandline string
return v1 + " " + v2 + " " + v3
您将在下面找到设置的界面。
# Create and name the window
root = Tk()
root.title("GUI - TEST VERSION")
# Set the variables needed for function
v1 = StringVar()
v2 = StringVar()
v3 = StringVar()
# Make text entry box
w = Label(root, text="V1")
w.pack()
text_entry = Entry(root, textvariable = v1.get())
text_entry.pack()
w = Label(root, text="V2")
w.pack()
text_entry = Entry(root, textvariable = v2.get())
text_entry.pack()
w = Label(root, text="V3")
w.pack()
text_entry = Entry(root, textvariable = v3.get())
text_entry.pack()
# Add a 'Run' button
b = Button(root, text="Run fuction", command= system_call(Command call, command(v1, v2,v3)))
b.pack()
# Call the GUI
root.mainloop()
收到一个错误,指出无法对字符串对象和实例对象进行分类。
【问题讨论】:
-
您的问题到底是什么?为什么要调用
get,而不是将StringVar对象本身分配给textvariable? -
command= system_call(command(v1, v2,v3))这将调用system_call,不将函数作为回调绑定到按钮。 -
我要调用系统调用! - 我收到一个错误,说字符串和实例对象不能被分类。
标签: python python-2.7 tkinter