【发布时间】:2017-05-18 15:59:33
【问题描述】:
我之前在 2.7 中使用 tkinter 接口构建了一个程序,我现在正在尝试将其合并到 3.5.2 程序中,但我遇到了很多错误,我不确定这是否是由于包更改在 Python3.5.2 的 tkinter 中。主要问题是与下面的下拉菜单有关,这将是我的 2.7 版本以及 3.5.2 的错误以及我尝试解决该错误的解决方案。
Tkinter 代码 Python 2.7:
from Tkinter import *
import tkMessageBox
OPTIONS = [
"Homepage",
"Instructions",
"Contact Page"
]
root = Tk()
root.title("Tittle")
root.geometry('700x300')
var = StringVar(root)
var.set("Menu")
#var.set(OPTIONS[0])
menu = apply(OptionMenu, (root, var) + tuple(OPTIONS))
menu.pack(side=TOP, anchor=W)
#Set the separator between the menu and the buttons
separator = Frame(height=2, bd=1, relief=SUNKEN)
separator.pack(fill=X, padx=1, pady=20)
top = Frame(root)
center = Frame(root)
bottom = Frame(root)
top.pack(side=TOP)
center.pack(side=TOP)
bottom.pack(side=BOTTOM, fill=BOTH, expand=True)
#Method to change the GUI when an option from the Menu is selected
def change_age(*args):
if var.get()=="Homepage":
b1.pack(in_=center, side=LEFT)
b2.pack(in_=center, side=LEFT)
b3.pack(in_=center, side=LEFT)
b4.pack(in_=center, side=LEFT)
if var.get()=="Instructions":
L1.pack(in_=center, side=LEFT)
var.trace('w', change_age)
# create the widgets for the top part of the GUI
b1 = Button(root, text="Database Parser", height=5)
b1.place(x=170, y=500)
b2 = Button(root, text="Web Crawler", height=5)
b3 = Button(root, text="Password Generator", height=5)
b4 = Button(root, text="Dictionary Attack", height=5)
#Instructions labels
L1 = Label(root, text="Instructions:\nHere you can write all your instructions")
L2 = Label(root, text="Contact Page:\nHere you can write all your contact information")
b1.pack(in_=center, side=LEFT)
b2.pack(in_=center, side=LEFT)
b3.pack(in_=center, side=LEFT)
b4.pack(in_=center, side=LEFT)
root.mainloop()
3.5.2 方案一:
from tkinter import *
from tkinter.filedialog import askopenfilenames
import sys
OPTIONS = [
'HOME'
'INSTRUCTIONS'
]
root = Tk()
root.title('Title')
root.geometry('700x300')
var = StringVar(root)
var.set("Menu")
menu = apply(OptionMenu, (root, var) + tuple(OPTIONS))
menu.pack(side=TOP, anchor=W)
separator = Frame(height=2, bd=1, relief=SUNKEN)
separator.pack()
top = Frame(root)
center = Frame(root)
bottom = Frame(root)
top.pack(side=TOP)
center.pack(side=TOP)
bottom.pack(side=BOTTOM, fill=BOTH, expand=True)
#Method changes GUI when an option from menu is selected
"""
def change_layout(*args):
if var.get()=='Homepage':
b1.pack(in_=center, side=LEFT)
b2.pack(in_=center, side=LEFT)
b3.pack(in_=center, side=LEFT)
if var.get()=='Instructions':
b1.pack_forget()
b2.pack_forget()
b3.pack_forget()
"""
def load_file():
fname = askopenfilenames(filetypes= (("Text Files", ".txt"),
("HTML Files", "*.html;*.htm"),
("All Files", "*.*")))
if fname:
try:
print('Files loaded')
except OSError as Error:
print('Files encountered error while loading')
# widgets for the top part of the GUI
b1 = Button(root, text='Select Files', height=5, command=load_file)
b1.place(x=170, y=500)
b2 = Button(root, text='Run Program', height=5)
b3 = Button(root, text='Save Results', height=5)
b1.pack(in_=center, SIDE=LEFT)
b2.pack(in_=center, SIDE=LEFT)
b3.pack(in_=center, SIDE=LEFT)
#Instructions - TODO Later date
root.mainloop()
解决方法一错误: 第一个问题发生在
menu = apply(OptionMenu, (root, var) + tuple(OPTIONS))
说明 NameError:未定义名称“应用”。我的第一个想法是删除它并继续认为在 python 3.5.2 中可能不需要它,然后它告诉我我无法运行代码“menu.pack(SIDE = TOP,anchor = W)”,错误代码:
AttributeError: 'tuple' object has no attribute 'pack'
3.5.2 方案二:
from tkinter import *
from tkinter.filedialog import askopenfilenames
import sys
class drop_down_menu(OptionMenu):
def __init__(self, master, menu, *options):
self.var = StringVar(master)
self.var.set('Menu')
OptionMenu.__init__(self, master, self.var, *options)
self.init_ui()
def init_ui(self):
self.master.title('Test')
self.pack(fill=BOTH, expand=1)
OPTIONS = [
'HOME'
'INSTRUCTIONS'
]
root = Tk()
root.title('Title')
root.geometry('700x300')
menu = drop_down_menu(root, 'Menu', OPTIONS)
menu.place
separator = Frame(height=2, bd=1, relief=SUNKEN)
separator.pack()
top = Frame(root)
center = Frame(root)
bottom = Frame(root)
top.pack(side=TOP)
center.pack(side=TOP)
bottom.pack(side=BOTTOM, fill=BOTH, expand=True)
#Method changes GUI when an option from menu is selected
"""
def change_layout(*args):
if var.get()=='Homepage':
b1.pack(in_=center, side=LEFT)
b2.pack(in_=center, side=LEFT)
b3.pack(in_=center, side=LEFT)
if var.get()=='Instructions':
b1.pack_forget()
b2.pack_forget()
b3.pack_forget()
"""
def load_file():
fname = askopenfilenames(filetypes= (("Text Files", ".txt"),
("HTML Files", "*.html;*.htm"),
("All Files", "*.*")))
if fname:
try:
print('Files loaded')
except OSError as Error:
print('Files encountered error while loading')
# widgets for the top part of the GUI
b1 = Button(root, text='Select Files', height=5, command=load_file)
b1.place(x=170, y=500)
b2 = Button(root, text='Run Program', height=5)
b3 = Button(root, text='Save Results', height=5)
b1.pack(in_=center, SIDE=LEFT)
b2.pack(in_=center, SIDE=LEFT)
b3.pack(in_=center, SIDE=LEFT)
#Instructions - TODO Later date
root.mainloop()
使用解决方案二,我能够通过菜单错误,但现在收到此错误消息,我只是不知道为什么这一切在 2.7 上都可以正常工作,但现在拒绝在 3.5.2 上执行任何操作:
Traceback (most recent call last):
File "C:/Users/Lewis Collins/PycharmProjects/Home.py", line 86, in <module>
b1.pack(in_=center, SIDE=LEFT)
File "C:\Users\Lewis Collins\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1991, in pack_configure
+ self._options(cnf, kw))
_tkinter.TclError: bad option "-SIDE": must be -after, -anchor, -before, -expand, -fill, -in, -ipadx, -ipady, -padx, -pady, or -side
提前感谢您的任何帮助或反馈。
【问题讨论】:
-
最后一个应该是小写的
side=。不是SIDE=。 -
关于apply。我没有时间形成完整的答案,所以我希望其他人会这样做。我只是提供一些资源。
-
@Lafexlos 谢谢我现在已经运行了,但快速提问我的菜单栏占据了整个区域,下拉菜单不会用新行分隔,任何关于如何缩小菜单的想法字符串“菜单”的长度
标签: python python-2.7 tkinter python-3.5