【发布时间】:2018-06-09 02:21:28
【问题描述】:
以下是网络教程中的一个小型 python3 脚本,演示了如何使用 Tkinter 制作下拉菜单栏。
直接从https://www.tutorialspoint.com/python3/tk_menu.htm复制过来,没有任何改动!
- 当我在树莓派上运行它(使用 3 个不同的 IDE)时,它运行良好
但是当我在我的 Mac 上(在 2 个不同的 IDE 中)运行它时,它只会创建白色窗口并且不会在其中放置任何菜单元素。
请注意,这台 Mac 能够运行我在尝试学习 Tkinter 时一直在制作的其他简单 Tkinter 脚本...制作按钮、弹出窗口,没问题。
新编辑:当我访问失败的 Mac 时,我会这样做:
>>> import tkinter
>>> tkinter._test()
...测试用它的小按钮等工作正常,我得到一个小窗口告诉我它是 v8.5。
这个脚本导致 mac 阻塞(而另一台计算机没有)是什么原因?非常感谢!
这是在 mac 上失败的 tkinter 脚本:
# !/usr/bin/python3
from tkinter import *
def donothing():
filewin = Toplevel(root)
button = Button(filewin, text="Do nothing button")
button.pack()
root = Tk()
menubar = Menu(root)
filemenu = Menu(menubar, tearoff = 0)
filemenu.add_command(label="New", command = donothing)
filemenu.add_command(label = "Open", command = donothing)
filemenu.add_command(label = "Save", command = donothing)
filemenu.add_command(label = "Save as...", command = donothing)
filemenu.add_command(label = "Close", command = donothing)
filemenu.add_separator()
filemenu.add_command(label = "Exit", command = root.quit)
menubar.add_cascade(label = "File", menu = filemenu)
editmenu = Menu(menubar, tearoff=0)
editmenu.add_command(label = "Undo", command = donothing)
editmenu.add_separator()
editmenu.add_command(label = "Cut", command = donothing)
editmenu.add_command(label = "Copy", command = donothing)
editmenu.add_command(label = "Paste", command = donothing)
editmenu.add_command(label = "Delete", command = donothing)
editmenu.add_command(label = "Select All", command = donothing)
menubar.add_cascade(label = "Edit", menu = editmenu)
helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label = "Help Index", command = donothing)
helpmenu.add_command(label = "About...", command = donothing)
menubar.add_cascade(label = "Help", menu = helpmenu)
root.config(menu = menubar)
root.mainloop()
【问题讨论】:
-
在 raspberrypi 和 macos 中有哪些版本的 tkinter?
-
哈希棒!如果这是直接从脚本中复制的,则 hashbang 标识符不正确,将在 python 的系统安装中运行,MacOS 的 IIRC 是 Python2,而不是 Python3。
-
嗨@Alan - 你知道,我也这么认为,但是当我在mac上完全删除hashbang文件时,文件仍然无法正常运行,同样的“空白窗口仍然出现运行它的主循环,因为它在我关闭它时宣布它已经完成”问题。 (并且所有其他在 Mac 上运行良好的 Tkinter 脚本都没有 hashbang。)
-
@eyllanesc -- 我会去尝试学习 tkinter 的版本。不好意思地说,我对在任一设置中找出这些版本号的方法都没有信心——我现在就试试。
-
是的,因为 hashbang 中有一个空格......所以它被忽略了。尝试将其更正为
#! /usr/bin/python3
标签: python python-3.x macos tkinter