简体   繁体   English

Python 3.5 Tkinter下拉菜单

[英]Python 3.5 tkinter drop down menu

I previously built a program in 2.7 with a tkinter interface I'm now trying to incorporate it into a 3.5.2 program but I'm having a lot of errors and I'm not sure if it is due to package changes within tkinter for Python3.5.2. 我以前在2.7中使用tkinter接口构建了一个程序,现在尝试将其合并到3.5.2程序中,但是我遇到了很多错误,而且我不确定这是否是由于tkinter中的软件包更改所致Python3.5.2。 The main problem is to do with the drop down menu below will be my 2.7 version along with the error for 3.5.2 and a solution i tried with the error. 主要问题是与下面的下拉菜单有关的是我的2.7版本以及3.5.2的错误以及我尝试过的错误解决方案。

Tkinter code Python 2.7: 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 Solution One: 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()

Solution One Errors: The first problem occurs with 解决方案一个错误:第一个问题发生在

menu = apply(OptionMenu, (root, var) + tuple(OPTIONS))

stating NameError: name 'apply' is not defined. 说明NameError:未定义名称“ apply”。 My first thought was to remove it and carry on thinking it may not be needed in python 3.5.2, then it tells me I can not run the code "menu.pack(SIDE=TOP, anchor=W)" with error code: 我首先想到的是删除它并继续认为它在python 3.5.2中可能不需要,然后它告诉我我无法运行带有错误代码的代码“ menu.pack(SIDE = TOP,anchor = W)”:

AttributeError: 'tuple' object has no attribute 'pack'

3.5.2 Solution 2: 3.5.2解决方案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()

Using Solution two i was able to get passed the menu errors but now receive this error messages and I'm just lost as to why this all worked without issue on 2.7 but now refuses to do anything on 3.5.2: 使用解决方案二,我能够通过菜单错误,但是现在收到此错误消息,而我却迷失了为什么这一切在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

Thanks in advance for any help or feedback. 在此先感谢您的帮助或反馈。

The only thing to adapt in the code in addition to the way to import tkinter libraries is the bit about the option menu: 除了导入tkinter库的方式之外,唯一需要在代码中进行调整的地方就是选项菜单:

original python2 code 原始的python2代码

menu = apply(OptionMenu, (root, var) + tuple(OPTIONS))

code working in python3 在python3中工作的代码

menu = OptionMenu(root, var, *OPTIONS) (this also works in python2) menu = OptionMenu(root, var, *OPTIONS) (这在python2中也有效)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM