2016-06-22 5 views
1

Ich wollte ein bisschen Tkinter lernen. Ich folgte ein paar Guides von sentdex auf Youtube und fühle mich wie ich ein gutes Verständnis der Grundlagen habe. Jetzt versuche ich jedoch, ein Skript zu schreiben, das als schnelles Protokoll funktioniert, und ich stehe auf Probleme, den Text aus dem Eintrag/text/optionmenu zu bekommen.Wie erhalte ich den Wert eines Artikels in tkinter, wenn ich eine Klasse verwende?

Wenn ich die GUI außerhalb der Klasse einrichten, kann ich einfach eine variable.get() und alles ist ducky. Aber wenn ich meine gui tkinter Sachen in einer Klasse habe, bekomme ich eine Ausnahme, die nicht definiert ist.

Hier ist mein Code. Das Problem liegt in def saveButtonCommand (self):

Wie bekomme ich die Werte von noteText und officeOption/Var?

# Imports 
from tkinter import * 
from tkinter import messagebox 
import sys, datetime 

class Window(Frame): 

    def __init__(self, master = None): 
     Frame.__init__(self, master) 
     self.master = master 
     self.init_window() 

    def init_window(self): 
     self.master.title("SPL - " + str(datetime.date.today())) 
     self.configure(background="#e7e7e7") 
     self.pack(fill=BOTH, expand=1) 

     # Menu -> Quit, Open Log 

     menu = Menu(self.master) 
     self.master.config(menu=menu) 

     file = Menu(menu) 
     file.add_command(label="Open Log") 
     file.add_command(label="Quit", command=self.client_exit) 
     menu.add_cascade(label="File", menu=file) 

     # Window Items 

     # Office Drop Down 
     OFFICES = [ 
      "County Building", 
      "Adult Probation", 
      "Building & Zoning", 
      "Circuit Court", 
      " - CC. Family Division", 
      " - CC. Friend of the Court", 
      " - CC. Juvenile Division", 
      "Clerk", 
      "Commissioners", 
      "Computer Infomration Systems", 
      "Corporation Counsel", 
      "District Court", 
      " - DC. Community Corrections", 
      " - DC. Magistrate Office", 
      " - DC. Probation Office", 
      "Economic Development Corp.", 
      "Equalization", 
      "Maintenance", 
      "Probate Court", 
      "Prosecuting Attorney", 
      "Public Guardian", 
      "Register of Deeds", 
      "Sheriff Department", 
      " - SD. Booking", 
      " - SD. Kitchen", 
      " - SD. Records", 
      " - SD. Squadroom", 
      "Social Security", 
      "Tax Mapping/GIS", 
      "Treasurer", 
      "Veteran's" 
     ] 

     officeLabel = Label(self, text="Office:", fg="#000000", bg="#e7e7e7", font="Verdana 10") 
     officeLabel.place(x=5,y=5) 

     officeOptionVar = StringVar() 
     officeOptionVar.set(OFFICES[0]) # Default Value 

     officeOption = OptionMenu(self, officeOptionVar, *OFFICES) 
     officeOption.place(x=5,y=28,width=310) 

     # Note 
     noteLabel = Label(self, text="Note:", fg="#000000", bg="#e7e7e7", font="Verdana 10") 
     noteLabel.place(x=5,y=58) 

     noteText = Text() 
     noteText.place(x=5, y=81, width=310, height=225) 

     # Save Button 
     saveButton = Button(self, text='Save', command=self.saveButtonCommand) 
     saveButton.place(x=5, y=312, width=310, height=50) 

    def client_exit(self): 
     exit() 

    def saveButtonCommand(self): 
     office = officeOptionVar.get() 
     note = noteText.get("1.0","end-1c") 
     messagebox.showinfo(title="Test", message=note) 

root = Tk() 
root.resizable(0,0) 

screen_width = root.winfo_screenwidth() 
screen_height = root.winfo_screenheight() 

geo_width = 320 
geo_height = 370 
geo_offset_w = int((screen_width/2)-(geo_width/2)) 

root.geometry(str(geo_width) + "x" + str(geo_height) + "+" + str(geo_offset_w) + "+200") 


app = Window(root) 
root.mainloop() 

Antwort

2

Nun, ich regelte es, sollten Sie selbst benutzen. „Variablenname“ ein zugängliches Attribut zu deklarieren.

from tkinter import * 
from tkinter import messagebox 
import sys, datetime 

class Window(Frame): 

def __init__(self, master = None): 
    Frame.__init__(self, master) 
    self.master = master 
    self.init_window() 

def init_window(self): 
    self.master.title("SPL - " + str(datetime.date.today())) 
    self.configure(background="#e7e7e7") 
    self.pack(fill=BOTH, expand=1) 

    # Menu -> Quit, Open Log 

    menu = Menu(self.master) 
    self.master.config(menu=menu) 

    file = Menu(menu) 
    file.add_command(label="Open Log") 
    file.add_command(label="Quit", command=self.client_exit) 
    menu.add_cascade(label="File", menu=file) 

    # Window Items 

    # Office Drop Down 
    OFFICES = [ 
     "County Building", 
     "Adult Probation", 
     "Building & Zoning", 
     "Circuit Court", 
     " - CC. Family Division", 
     " - CC. Friend of the Court", 
     " - CC. Juvenile Division", 
     "Clerk", 
     "Commissioners", 
     "Computer Infomration Systems", 
     "Corporation Counsel", 
     "District Court", 
     " - DC. Community Corrections", 
     " - DC. Magistrate Office", 
     " - DC. Probation Office", 
     "Economic Development Corp.", 
     "Equalization", 
     "Maintenance", 
     "Probate Court", 
     "Prosecuting Attorney", 
     "Public Guardian", 
     "Register of Deeds", 
     "Sheriff Department", 
     " - SD. Booking", 
     " - SD. Kitchen", 
     " - SD. Records", 
     " - SD. Squadroom", 
     "Social Security", 
     "Tax Mapping/GIS", 
     "Treasurer", 
     "Veteran's" 
    ] 

    officeLabel = Label(self, text="Office:", fg="#000000", bg="#e7e7e7", font="Verdana 10") 
    officeLabel.place(x=5,y=5) 

    self.officeOptionVar = StringVar() 
    self.officeOptionVar.set(OFFICES[0]) # Default Value 

    officeOption = OptionMenu(self, self.officeOptionVar, *OFFICES) 
    officeOption.place(x=5,y=28,width=310) 

    # Note 
    noteLabel = Label(self, text="Note:", fg="#000000", bg="#e7e7e7",  font="Verdana 10") 
    noteLabel.place(x=5,y=58) 

    self.noteText = Text() 
    self.noteText.place(x=5, y=81, width=310, height=225) 

    # Save Button 
    saveButton = Button(self, text='Save', command=self.saveButtonCommand) 
    saveButton.place(x=5, y=312, width=310, height=50) 

def client_exit(self): 
    exit() 

def saveButtonCommand(self): 
    office = self.officeOptionVar.get() 
    note = self.noteText.get("1.0","end-1c") 
    messagebox.showinfo(title="Test", message=note) 

root = Tk() 
root.resizable(0,0) 

screen_width = root.winfo_screenwidth() 
screen_height = root.winfo_screenheight() 

geo_width = 320 
geo_height = 370 
geo_offset_w = int((screen_width/2)-(geo_width/2)) 

root.geometry(str(geo_width) + "x" + str(geo_height) + "+" + str(geo_offset_w) + "+200") 


app = Window(root) 
root.mainloop() 
+0

Vielen Dank! Ich probierte jede Kombination aus dem Hinzufügen von Selbst zu der saveButtonCommand-Funktion, die ich nicht dachte, um es den Variablen selbst hinzuzufügen. Wald durch die Bäume. –

Verwandte Themen