Domanda

I am new to Aptana, and have searched the net to see how it handles gui development.

I have tried the following code. It shows no bugs in the console. However, it will not give me the Tk() window named myGui.

from Tkinter import *
import ttk

def main():
    myGui = Tk()
    myGui.title("My Gui New")
    myGui.geometry('400x200+200+200')

    l = Label(myGui, text="Help")
    l.pack()

if __name__ == "__main__":
    main()   

Any pointers. I able to get my functions to run in the console, but this Gui development is not working out so well.

È stato utile?

Soluzione

The-IT is right, you do want to add myGui.mainloop() to the end of main.

Normally when I'm working in Tkinter I try to move some of the information in your function main into the if... clause. This makes larger, more complex interfaces much easier to handle.

A good start is to use a class:

from Tkinter import *
from ttk import *

class App(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        self.l = Label(self, text='Help')
        self.l.pack()

if __name__ == '__main__':
    root = Tk()
    app = App(root)
    root.mainloop()

The benefits of this is that you can easily introduce toplevels, add additonal widgets (in an organized fashion) and have clearer code. You can also use the classes to make templates for other widgets; in this case, you are building a special frame class and giving it whatever attributes you want. The same can be done for buttons, entries, and other frames. I'm not familiar with aptana3, but this should work with no problem. Just make sure your indentation is consistent.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top