《Python3.0》Radiobutton Widget #2 ★★

Python.use(better, Tkinter)記事一覧
Radiobutton Widget #2

《著》森こねこ+小粒ちゃん《監修》小泉ひよ子とタマゴ倶楽部
第1版♪2003/05/23 ● 第2版♪2006/11/28 ● 第3版♪2009/05/12

事例:Radiobutton Widget

モジュールを起動すると、次のようなウィンドウが現れます。

$ python3.0 exRadiobutton2.py



概要:Radiobutton Widget

>>> print(Radiobutton.__doc__)
Radiobutton widget which shows only one of several buttons in on-state.
>>> print(Radiobutton.__init__.__doc__)
Construct a radiobutton widget with the parent MASTER.

        Valid resource names: activebackground, activeforeground, anchor,
        background, bd, bg, bitmap, borderwidth, command, cursor,
        disabledforeground, fg, font, foreground, height,
        highlightbackground, highlightcolor, highlightthickness, image,
        indicatoron, justify, padx, pady, relief, selectcolor, selectimage,
        state, takefocus, text, textvariable, underline, value, variable,
        width, wraplength.

コードの解説:

#! /usr/bin/Python3.0
from tkinter import *

class Ex(Frame):
    def __init__(self, master = None):
        super().__init__(master)
        Pack.config(self)
        self.createWidgets()

    def createWidgets(self):
        self.value = StringVar()
        self.value.set("-- none --")

        self.buttonFrame = Frame(self)

        for e in "red", "green", "blue":
            button = Radiobutton(
                self.buttonFrame,
                text = e,
                variable = self.value,
                value = e,
                command = self.draw,
                )
            setattr(self.buttonFrame, e, button)

        self.buttonFrame.entry = Entry(
            self.buttonFrame,
            textvariable = self.value,
            )
        
        button = Button(
            self.buttonFrame,
            text = 'QUIT',
            command = self.quit,
            )

        canvas = Canvas(
            self,
            width = "5c", height = "4c",
            )
        canvas.create_rectangle(
            0, 0, "5c", "4c", fill = "black",
            )
        self.canvas = canvas

        ## ----------------------------------------
        self.buttonFrame.pack(side = LEFT)
        self.buttonFrame.red.pack(anchor = W)
        self.buttonFrame.green.pack(anchor = W)
        self.buttonFrame.blue.pack(anchor = W)
        self.buttonFrame.entry.pack(fill = X)
        button.pack(fill = BOTH)
        canvas.pack(side = RIGHT)

    def draw(self):
        e = self.canvas.create_rectangle(
            0, 0, "3i", "3i",
            fill = self.value.get(),
            )

## ----------------------------------------
def example(*args):
    root = Tk()
    root.title('Radiobutton')
    Ex(root)
    root.mainloop()

## ----------------------------------------
if __name__=='__main__':
    example()

Tips

》作業中です《

Last updated♪09/06/07