2017-01-04 4 views
0

Ich habe zuvor ein Programm in 2.7 mit einem tkinter-Interface gebaut Ich versuche jetzt, es in ein 3.5.2-Programm zu integrieren, aber ich habe viele Fehler und ich bin mir nicht sicher, ob Es liegt an Paketänderungen innerhalb von tkinter für Python 3.5.2. Das Hauptproblem ist, mit dem Dropdown-Menü unten zu tun, wird meine 2.7-Version zusammen mit dem Fehler für 3.5.2 und eine Lösung sein, die ich mit dem Fehler versuchte.Python 3.5 tkinter Dropdown-Menü

Tkinter Code 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 Lösung One:

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() 

Lösung Ein Fehler: Das erste Problem tritt bei

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

Nameerror besagt: Name 'Anwenden' ist nicht definiert. Mein erster Gedanke war, es zu entfernen und weiter zu denken, dass es in Python 3.5.2 nicht benötigt wird, dann sagt es mir, dass ich den Code "menu.pack (SIDE = TOP, anchor = W)" mit Fehlercode nicht ausführen kann:

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

3.5.2 Lösung 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() 

Mit Lösung zwei ich in der Lage war, das Menü Fehler weitergegeben werden, aber jetzt diese Fehlermeldungen angezeigt werden, und ich bin nur, warum dies alles funktioniert verloren ohne Problem auf 2.7 aber jetzt weigert sich, etwas auf 3.5.2 zu tun:

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 

Vielen Dank im Voraus für jede Hilfe oder Rückmeldung.

+0

Es sollte Kleinbuchstaben 'side =' für die letzte sein. Nicht 'SIDE ='. – Lafexlos

+0

Über [apply] (http://python3porting.com/differences.html#apply). Ich habe keine Zeit, eine vollständige Antwort zu formulieren. Ich hoffe, dass es jemand anderes tut. Ich gebe nur einige Ressourcen ein. – Lafexlos

+0

@Lafexlos Danke Ich habe es jetzt ausgeführt, aber schnelle Frage meine Menüleiste nimmt die ganze Region ein und die Dropdowns werden sich nicht durch neue Zeilen trennen, irgendeine Idee, wie man das Menü auf die Länge des Strings 'Menü' verkleinert –

Antwort

1

Das einzige, was im Code zusätzlich zu der Art und Weise anzupassen tkinter Bibliotheken zu importieren ist das Bit über das Optionsmenü:

original python2 Code

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

Code arbeiten in python3

menu = OptionMenu(root, var, *OPTIONS) (dies funktioniert auch in python2)