开发者

Simple python Tkinter questions about buttons

Can someone provide me with some example code. I am fairly fluent with python but can't figure this out. So i will be generating a list with say "x" elements from other code. I need Tkinter to display a "x" buttons that can be checked on or off. Then on开发者_如何学JAVAce the user has selected whichever ones they want, they will press GO and more code will execute on only the items in the list that are selected. So basically i just need to make something True or False (or 1 or 0) by using the checkbuttons in Tkinter. If someone can show me how to do this using Classes id love to see it. Thanks!!


import Tkinter as tk

def printVar():
    print 'var is', var.get()

root = tk.Tk()
var = tk.IntVar()
c = tk.Checkbutton(root, text='Check me', variable=var, command=printVar)
c.pack()
root.mainloop()

Take a look at Tkinter page at the python wiki.

Edit

import Tkinter as tk

def printOpts():
    for opt, val in zip(options, checkboxes):
        print opt + ': ' + str(bool(val.get()))

options = ['eggs', 'apples', 'pears']
checkboxes = []

root = tk.Tk()
for opt in options:
    v = tk.IntVar()
    checkboxes.append(v)
    c = tk.Checkbutton(root, text=opt, variable=v)
    c.pack()

btn = tk.Button(root, text='Print options', command=printOpts)
btn.pack()
root.mainloop()


Makes a nice toggle button

import Tkinter


class TkToggle(Tkinter.Tk):
    def __init__(self, parent):
        Tkinter.Tk.__init__(self, parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        global toggle
        toggle = 0

        self.Button = Tkinter.Label(self, text='X', relief='ridge')
        self.Button.pack(ipadx=15,ipady=15)
        self.Button.bind('<ButtonRelease-1>', self.Toggle)

    def Toggle (self, event):
        global toggle

        if toggle == 0:
            toggle = 1
            self.Button.configure(text = '')
            print 'A'

        else:
            toggle = 0
            self.Button.configure(text = 'X')
            print 'B'

if __name__ == "__main__":
    app = TkToggle(None)
    app.mainloop()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜