Python 如何从tkinter应用程序菜单单击打开文件?
我已经对菜单进行了编程,当前单击菜单项时,它会打印(菜单中的某些文本) 我需要这个菜单单击选项来打开python文件,而不是打印文件名 这似乎是一个非常简单的问题,但我对python还不熟悉,还在学习 我找不到完全适合这种情况的方法。我尝试了Python 如何从tkinter应用程序菜单单击打开文件?,python,windows,tkinter,Python,Windows,Tkinter,我已经对菜单进行了编程,当前单击菜单项时,它会打印(菜单中的某些文本) 我需要这个菜单单击选项来打开python文件,而不是打印文件名 这似乎是一个非常简单的问题,但我对python还不熟悉,还在学习 我找不到完全适合这种情况的方法。我尝试了popen和execfile,但运气不佳。;虽然我不确定我是否正确使用了它们 import tkinter def set_menu(window, choices): menubar = tkinter.Menu(root) window
popen
和execfile
,但运气不佳。;虽然我不确定我是否正确使用了它们
import tkinter
def set_menu(window, choices):
menubar = tkinter.Menu(root)
window.config(menu=menubar)
def _set_choices(menu, choices):
for label, command in choices.items():
if isinstance(command, dict):
# Submenu
submenu = tkinter.Menu(menu)
menu.add_cascade(label=label, menu=submenu)
_set_choices(submenu, command)
elif label == '-' and command == '-':
# Separator
menu.add_separator()
else:
# Simple choice
menu.add_command(label=label, command=command)
_set_choices(menubar, choices)
if __name__ == '__main__':
import sys
root = tkinter.Tk()
from collections import OrderedDict
set_menu(root, {
'Table of Contents': OrderedDict([
('Ecclesiastes', lambda: print('Ecclesiastes.py')),
('Ecclesiasticus', lambda: print('ecclesiaticus.exe')),
('-', '-'),
('Quit', lambda: sys.exit(0))
])
})
root.mainloop()
我希望通过执行“ecclesiastes.py
”和“ecclesiasticus.exe
”的几种不同方法来打开它,但不幸的是,错误消息实际上只是告诉我,我不知道自己被卡住了什么,而不是关于如何获得执行这两个文件所需的正确代码的线索。我将popen
放在.py
文件之前,将execfile
放在.exe
文件之前,但我认为这样做不太正确
我将print放在两个文件名的前面,这样其他人就可以在这里指出正确的命令,因为我认为
execfile
或popen
对于这段代码是不正确的。stovfl的注释是一个很好的解决方案
我经常使用:
os.startfile('文件名')
我无法告诉您这是否是最好的选择,但它确实有效:)
您的代码如下所示:
import tkinter
import os
def set_menu(window, choices):
menubar = tkinter.Menu(root)
window.config(menu=menubar)
def _set_choices(menu, choices):
for label, command in choices.items():
if isinstance(command, dict):
# Submenu
submenu = tkinter.Menu(menu)
menu.add_cascade(label=label, menu=submenu)
_set_choices(submenu, command)
elif label == '-' and command == '-':
# Separator
menu.add_separator()
else:
# Simple choice
menu.add_command(label=label, command=command)
_set_choices(menubar, choices)
if __name__ == '__main__':
import sys
root = tkinter.Tk()
from collections import OrderedDict
set_menu(root, {
'Table of Contents': OrderedDict([
('Ecclesiastes', lambda: os.startfile('Ecclesiastes.py')),
('Ecclesiasticus', lambda: os.startfile('ecclesiaticus.exe')),
('-', '-'),
('Quit', lambda: sys.exit(0))
])
})
root.mainloop()
import os
import shlex
import subprocess
import sys
import tkinter
def set_menu(window, choices):
menubar = tkinter.Menu(root)
window.config(menu=menubar)
def _set_choices(menu, choices):
for label, command in choices.items():
if isinstance(command, dict):
# Submenu
submenu = tkinter.Menu(menu)
menu.add_cascade(label=label, menu=submenu)
_set_choices(submenu, command)
elif label == '-' and command == '-':
# Separator
menu.add_separator()
else:
# Simple choice
menu.add_command(label=label, command=command)
_set_choices(menubar, choices)
def exec_command(command):
cmd = shlex.split(command)
print('subprocess({})'.format(cmd))
subprocess.run(cmd, shell=True)
if __name__ == '__main__':
from collections import OrderedDict
import sys
root = tkinter.Tk()
set_menu(root, {'Table of Contents':
OrderedDict([
('Ecclesiastes',
lambda: exec_command('Ecclesiastes.py')),
('Ecclesiasticus',
lambda: exec_command('ecclesiaticus.exe -arg 42')),
('-', '-'),
('Quit',
lambda: sys.exit(0))
])
})
root.mainloop()
我希望这能解决你的问题 stovfl的评论是一个很好的解决方案 我经常使用: os.startfile('文件名') 我无法告诉您这是否是最好的选择,但它确实有效:) 您的代码如下所示:
import tkinter
import os
def set_menu(window, choices):
menubar = tkinter.Menu(root)
window.config(menu=menubar)
def _set_choices(menu, choices):
for label, command in choices.items():
if isinstance(command, dict):
# Submenu
submenu = tkinter.Menu(menu)
menu.add_cascade(label=label, menu=submenu)
_set_choices(submenu, command)
elif label == '-' and command == '-':
# Separator
menu.add_separator()
else:
# Simple choice
menu.add_command(label=label, command=command)
_set_choices(menubar, choices)
if __name__ == '__main__':
import sys
root = tkinter.Tk()
from collections import OrderedDict
set_menu(root, {
'Table of Contents': OrderedDict([
('Ecclesiastes', lambda: os.startfile('Ecclesiastes.py')),
('Ecclesiasticus', lambda: os.startfile('ecclesiaticus.exe')),
('-', '-'),
('Quit', lambda: sys.exit(0))
])
})
root.mainloop()
import os
import shlex
import subprocess
import sys
import tkinter
def set_menu(window, choices):
menubar = tkinter.Menu(root)
window.config(menu=menubar)
def _set_choices(menu, choices):
for label, command in choices.items():
if isinstance(command, dict):
# Submenu
submenu = tkinter.Menu(menu)
menu.add_cascade(label=label, menu=submenu)
_set_choices(submenu, command)
elif label == '-' and command == '-':
# Separator
menu.add_separator()
else:
# Simple choice
menu.add_command(label=label, command=command)
_set_choices(menubar, choices)
def exec_command(command):
cmd = shlex.split(command)
print('subprocess({})'.format(cmd))
subprocess.run(cmd, shell=True)
if __name__ == '__main__':
from collections import OrderedDict
import sys
root = tkinter.Tk()
set_menu(root, {'Table of Contents':
OrderedDict([
('Ecclesiastes',
lambda: exec_command('Ecclesiastes.py')),
('Ecclesiasticus',
lambda: exec_command('ecclesiaticus.exe -arg 42')),
('-', '-'),
('Quit',
lambda: sys.exit(0))
])
})
root.mainloop()
我希望这能解决你的问题 因为它看起来像是您实际想要执行的文件,所以您可以这样做:
import tkinter
import os
def set_menu(window, choices):
menubar = tkinter.Menu(root)
window.config(menu=menubar)
def _set_choices(menu, choices):
for label, command in choices.items():
if isinstance(command, dict):
# Submenu
submenu = tkinter.Menu(menu)
menu.add_cascade(label=label, menu=submenu)
_set_choices(submenu, command)
elif label == '-' and command == '-':
# Separator
menu.add_separator()
else:
# Simple choice
menu.add_command(label=label, command=command)
_set_choices(menubar, choices)
if __name__ == '__main__':
import sys
root = tkinter.Tk()
from collections import OrderedDict
set_menu(root, {
'Table of Contents': OrderedDict([
('Ecclesiastes', lambda: os.startfile('Ecclesiastes.py')),
('Ecclesiasticus', lambda: os.startfile('ecclesiaticus.exe')),
('-', '-'),
('Quit', lambda: sys.exit(0))
])
})
root.mainloop()
import os
import shlex
import subprocess
import sys
import tkinter
def set_menu(window, choices):
menubar = tkinter.Menu(root)
window.config(menu=menubar)
def _set_choices(menu, choices):
for label, command in choices.items():
if isinstance(command, dict):
# Submenu
submenu = tkinter.Menu(menu)
menu.add_cascade(label=label, menu=submenu)
_set_choices(submenu, command)
elif label == '-' and command == '-':
# Separator
menu.add_separator()
else:
# Simple choice
menu.add_command(label=label, command=command)
_set_choices(menubar, choices)
def exec_command(command):
cmd = shlex.split(command)
print('subprocess({})'.format(cmd))
subprocess.run(cmd, shell=True)
if __name__ == '__main__':
from collections import OrderedDict
import sys
root = tkinter.Tk()
set_menu(root, {'Table of Contents':
OrderedDict([
('Ecclesiastes',
lambda: exec_command('Ecclesiastes.py')),
('Ecclesiasticus',
lambda: exec_command('ecclesiaticus.exe -arg 42')),
('-', '-'),
('Quit',
lambda: sys.exit(0))
])
})
root.mainloop()
由于看起来您实际上想要执行该文件,您可以这样做:
import tkinter
import os
def set_menu(window, choices):
menubar = tkinter.Menu(root)
window.config(menu=menubar)
def _set_choices(menu, choices):
for label, command in choices.items():
if isinstance(command, dict):
# Submenu
submenu = tkinter.Menu(menu)
menu.add_cascade(label=label, menu=submenu)
_set_choices(submenu, command)
elif label == '-' and command == '-':
# Separator
menu.add_separator()
else:
# Simple choice
menu.add_command(label=label, command=command)
_set_choices(menubar, choices)
if __name__ == '__main__':
import sys
root = tkinter.Tk()
from collections import OrderedDict
set_menu(root, {
'Table of Contents': OrderedDict([
('Ecclesiastes', lambda: os.startfile('Ecclesiastes.py')),
('Ecclesiasticus', lambda: os.startfile('ecclesiaticus.exe')),
('-', '-'),
('Quit', lambda: sys.exit(0))
])
})
root.mainloop()
import os
import shlex
import subprocess
import sys
import tkinter
def set_menu(window, choices):
menubar = tkinter.Menu(root)
window.config(menu=menubar)
def _set_choices(menu, choices):
for label, command in choices.items():
if isinstance(command, dict):
# Submenu
submenu = tkinter.Menu(menu)
menu.add_cascade(label=label, menu=submenu)
_set_choices(submenu, command)
elif label == '-' and command == '-':
# Separator
menu.add_separator()
else:
# Simple choice
menu.add_command(label=label, command=command)
_set_choices(menubar, choices)
def exec_command(command):
cmd = shlex.split(command)
print('subprocess({})'.format(cmd))
subprocess.run(cmd, shell=True)
if __name__ == '__main__':
from collections import OrderedDict
import sys
root = tkinter.Tk()
set_menu(root, {'Table of Contents':
OrderedDict([
('Ecclesiastes',
lambda: exec_command('Ecclesiastes.py')),
('Ecclesiasticus',
lambda: exec_command('ecclesiaticus.exe -arg 42')),
('-', '-'),
('Quit',
lambda: sys.exit(0))
])
})
root.mainloop()
读啊读啊