Note that there are some explanatory texts on larger screens.

plurals
  1. POpython beginner: interaction of classes
    primarykey
    data
    text
    <p>What is the problem in the below code? I think due to the last argument it throws exception when calling menubar?</p> <p>The app is supposed to be a simple text editor and I want to have the menu declaration in a separate class. When I run the script, the textfield gets generated successfully, but without menubar. When I quit the application, the following error message occurs.</p> <pre><code>""" Traceback (most recent call last): File "Editor_play.py", line 41, in &lt;module&gt; menu = Menubar(window, textfield.text) File "Editor_play.py", line 20, in __init__ menubar = Menu(window) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 2580, in __init__ File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1974, in __init__ _tkinter.TclError: this isn't a Tk applicationNULL main window """ from Tkinter import * import tkFileDialog class Textfield(object): def __init__(self, window): self.window = window window.title("text editor") self.scrollbar = Scrollbar(window) self.scrollbar.pack(side="right",fill="y") self.text = Text(window,yscrollcommand=self.scrollbar.set) self.scrollbar.config(command=self.text.yview) self.text.pack() window.mainloop() class Menubar(object): def __init__(self, window, text): self.window = window self.text = text menubar = Menu(window) filemenu = Menu(menubar) filemenu.add_command(label="load",command=self.load) filemenu.add_command(label="save as",command=self.saveas) menubar.add_cascade(label="file",menu=filemenu) window.config(menu=menubar) def load(self): self.file = tkFileDialog.askopenfile() self.text.delete(1.0, END) if self.file: self.text.insert(1.0, self.file.read()) def saveas(self): self.file = tkFileDialog.asksaveasfile() if self.file: self.file.write(self.text.get(1.0, END)) window = Tk() textfield = Textfield(window) menu = Menubar(window, textfield.text) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload