在我的代码中添加菜单栏时 Python 中的 TkInter 错误

Posted

技术标签:

【中文标题】在我的代码中添加菜单栏时 Python 中的 TkInter 错误【英文标题】:TkInter bug in Python while adding a menu bar to my code 【发布时间】:2017-03-26 16:59:14 【问题描述】:

我找到了一些在 TkInter 中创建菜单栏的代码,但我无法输入我的代码而不会出现问题。

这是我正在使用的代码:

from Tkinter import *
import subprocess
import os
master = Tk()
master.geometry("1000x668")
master.title("Menu")
master.configure(background='pale green')
master.iconbitmap(r"C:\Users\André\Desktop\python\menu.ico")
master = Tk()

def NewFile():
    print "New File!"
def OpenFile():
    name = askopenfilename()
    print name
def About():
    print "This is a simple example of a menu"
menu = Menu(master)
master.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=NewFile)
filemenu.add_command(label="Open...", command=OpenFile)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=master.quit)

w = Label(master, text="Abrir", bg="pale green", fg="steel blue", font=("Algerian", 20, "bold"))
w.pack()
w.place(x=100, y=0)
def notepad():
    subprocess.Popen("notepad.exe")
buttonote = Button(master, text="Bloco de notas", wraplength=50, justify=CENTER, padx=2, bg="light sea green", height=2, width=7, command=notepad)
buttonote.pack()
buttonote.place(x=0, y=50)
def regedit():
    subprocess.Popen("regedit.exe")
buttonreg = Button(master, text="Editor de Registo", wraplength=50, justify=CENTER, padx=2, bg="light sea green", height=2, width=7, command=regedit)
buttonreg.pack()
buttonreg.place(x=60, y=50)
def skype():
    subprocess.Popen("skype.exe")
buttonskype = Button(master, text="Skype", bg="light sea green", height=2, width=7, command=skype)
buttonskype.pack()
buttonskype.place(x=120, y=50)
def steam():
    os.startfile("D:\Steam\Steam.exe")
buttonsteam = Button(master, text="Steam", bg="light sea green", height=2, width=7, command=steam)
buttonsteam.pack()
buttonsteam.place(x=178, y=50)
e1 = Entry(master, width=15)
e1.pack(padx=100,pady=4, ipadx=2)
def save():
    text = e1.get()
    SaveFile = open('information.txt','w')
    SaveFile.write(text)
    SaveFile.close()
nome = Label(master, text="Nome?", bg="pale green", fg="steel blue", font=("Arial Black", 12))
nome.pack()
nome.place(x=380, y=0)
buttonsave = Button(master, text="Guardar", bg="light sea green", height=1, width=6, command=save)
buttonsave.pack()
buttonsave.place(x=550, y=0)

f = open('information.txt','r')
line = f.readline()
show = Label(master, text=line, bg="pale green", fg="steel blue", font=("Arial Black", 12))
show.pack()
show.place(x=32, y=640)
hi = Label(master, text='Hi, ', bg="pale green", fg="steel blue", font=("Arial Black", 12))
hi.pack()
hi.place(x=0, y=640)
master.mainloop()

谁能弄清楚我的代码有什么问题?谢谢!

【问题讨论】:

【参考方案1】:

你在第 4 行有 master = Tk() 并在第 9 行重复。删除第 9 行。

【讨论】:

以上是关于在我的代码中添加菜单栏时 Python 中的 TkInter 错误的主要内容,如果未能解决你的问题,请参考以下文章

Python - “为字段添加属性”有啥作用?

如何在选项菜单中添加 YouTube 链接?

如何在我的程序中添加 PyQt 菜单栏?

如何在 Tkinter 中将单选按钮添加到子菜单

Tkinter实现窗口菜单栏

在我的WordPress菜单项中添加#无法正常工作