如何在python gui中将从菜单按钮选择的值作为输入提供给os.system
下面的python gui代码我正在尝试从下拉菜单按钮(图形和密度)中选择值,并尝试将它们作为命令行参数传递给readfile()函数中的os.system命令,如下所示,但我在将从下拉菜单中选择的值传递给os.system命令时遇到问题 导入操作系统 将Tkinter作为tk导入如何在python gui中将从菜单按钮选择的值作为输入提供给os.system,python,python-2.7,tkinter,subprocess,Python,Python 2.7,Tkinter,Subprocess,下面的python gui代码我正在尝试从下拉菜单按钮(图形和密度)中选择值,并尝试将它们作为命令行参数传递给readfile()函数中的os.system命令,如下所示,但我在将从下拉菜单中选择的值传递给os.system命令时遇到问题 导入操作系统 将Tkinter作为tk导入 def buttonClicked(btn): density= btn def graphselected(graphbtn): graph=graphbtn def readfile():
def buttonClicked(btn):
density= btn
def graphselected(graphbtn):
graph=graphbtn
def readfile():
os.system( 'python C:Desktop/python/ABC.py graph density')
root = tk.Tk()
root.title("Dense Module Enumeration")
btnList=[0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0]
btnMenu = tk.Menubutton(root, text='Density')
contentMenu = tk.Menu(btnMenu)
btnMenu.config(menu=contentMenu)
for btn in btnList:
contentMenu.add_command(label=btn, command = lambda btn=btn: buttonClicked(btn))
btnMenu.pack()
graph_list=['graph1.txt','graph2.txt','graph3.txt','graph.txt']
btnMenu = tk.Menubutton(root, text='graph')
contentMenu = tk.Menu(btnMenu)
btnMenu.config(menu=contentMenu)
for btn in graph_list:
contentMenu.add_command(label=btn, command =lambda btn= btn: graphselected(btn))
btnMenu.pack()
button = tk.Button(root, text="DME", command=readfile)
button.pack()
root.mainloop()
它很容易实现-为每个按钮的功能应用所需的值。以下是一个示例:
from functools import partial
import Tkinter as tk
BTNLIST = [0.0, 0.1, 0.2]
def btn_clicked(payload=None):
"""Just prints out given payload."""
print('Me was clicked. Payload: {}'.format(payload))
def init_controls():
"""Prepares GUI controls and starts mainloop"""
root = tk.Tk()
menu = tk.Menu(root)
root.config(menu=menu)
sample_menu = tk.Menu(menu)
menu.add_cascade(label="Destiny", menu=sample_menu)
for btn_value in BTNLIST:
sample_menu.add_command(
label=btn_value,
# Here is the trick with partial
command=partial(btn_clicked, btn_value)
)
root.mainloop()
init_controls()
它很容易实现-为每个按钮的功能应用所需的值。以下是一个示例:
from functools import partial
import Tkinter as tk
BTNLIST = [0.0, 0.1, 0.2]
def btn_clicked(payload=None):
"""Just prints out given payload."""
print('Me was clicked. Payload: {}'.format(payload))
def init_controls():
"""Prepares GUI controls and starts mainloop"""
root = tk.Tk()
menu = tk.Menu(root)
root.config(menu=menu)
sample_menu = tk.Menu(menu)
menu.add_cascade(label="Destiny", menu=sample_menu)
for btn_value in BTNLIST:
sample_menu.add_command(
label=btn_value,
# Here is the trick with partial
command=partial(btn_clicked, btn_value)
)
root.mainloop()
init_controls()
它很容易实现-为每个按钮的功能应用所需的值。以下是一个示例:
from functools import partial
import Tkinter as tk
BTNLIST = [0.0, 0.1, 0.2]
def btn_clicked(payload=None):
"""Just prints out given payload."""
print('Me was clicked. Payload: {}'.format(payload))
def init_controls():
"""Prepares GUI controls and starts mainloop"""
root = tk.Tk()
menu = tk.Menu(root)
root.config(menu=menu)
sample_menu = tk.Menu(menu)
menu.add_cascade(label="Destiny", menu=sample_menu)
for btn_value in BTNLIST:
sample_menu.add_command(
label=btn_value,
# Here is the trick with partial
command=partial(btn_clicked, btn_value)
)
root.mainloop()
init_controls()
它很容易实现-为每个按钮的功能应用所需的值。以下是一个示例:
from functools import partial
import Tkinter as tk
BTNLIST = [0.0, 0.1, 0.2]
def btn_clicked(payload=None):
"""Just prints out given payload."""
print('Me was clicked. Payload: {}'.format(payload))
def init_controls():
"""Prepares GUI controls and starts mainloop"""
root = tk.Tk()
menu = tk.Menu(root)
root.config(menu=menu)
sample_menu = tk.Menu(menu)
menu.add_cascade(label="Destiny", menu=sample_menu)
for btn_value in BTNLIST:
sample_menu.add_command(
label=btn_value,
# Here is the trick with partial
command=partial(btn_clicked, btn_value)
)
root.mainloop()
init_controls()
按照您的方式,
graph
和density
是graphselected()
和buttonClicked()
的局部变量。因此,readfile()
永远无法访问这些变量,除非在所有三个函数中将它们声明为全局变量
然后,您需要格式化一个字符串,以合并图形
和密度
中的值。您可以使用字符串来实现这一点
将这三个功能结合起来
def buttonClicked(btn):
global density
density = btn
def graphselected(graphbtn):
global graph
graph = graphbtn
def readfile():
global density, graph
os.system('python C:Desktop/python/ABC.py {} {}'.format(graph, density))
按照您的方式,
graph
和density
是graphselected()
和buttonClicked()
的局部变量。因此,readfile()
永远无法访问这些变量,除非在所有三个函数中将它们声明为全局变量
然后,您需要格式化一个字符串,以合并图形
和密度
中的值。您可以使用字符串来实现这一点
将这三个功能结合起来
def buttonClicked(btn):
global density
density = btn
def graphselected(graphbtn):
global graph
graph = graphbtn
def readfile():
global density, graph
os.system('python C:Desktop/python/ABC.py {} {}'.format(graph, density))
按照您的方式,
graph
和density
是graphselected()
和buttonClicked()
的局部变量。因此,readfile()
永远无法访问这些变量,除非在所有三个函数中将它们声明为全局变量
然后,您需要格式化一个字符串,以合并图形
和密度
中的值。您可以使用字符串来实现这一点
将这三个功能结合起来
def buttonClicked(btn):
global density
density = btn
def graphselected(graphbtn):
global graph
graph = graphbtn
def readfile():
global density, graph
os.system('python C:Desktop/python/ABC.py {} {}'.format(graph, density))
按照您的方式,
graph
和density
是graphselected()
和buttonClicked()
的局部变量。因此,readfile()
永远无法访问这些变量,除非在所有三个函数中将它们声明为全局变量
然后,您需要格式化一个字符串,以合并图形
和密度
中的值。您可以使用字符串来实现这一点
将这三个功能结合起来
def buttonClicked(btn):
global density
density = btn
def graphselected(graphbtn):
global graph
graph = graphbtn
def readfile():
global density, graph
os.system('python C:Desktop/python/ABC.py {} {}'.format(graph, density))