Python语言程序设计Y.Daniel Liang练习题Chapter9

ckp9.4-9.5

'''
ckp9.4
window = Tk()

ckp9.5
callback functions handlers

'''

ckp9.6-9.11

'''
ckp9.6
label = Label(window, text = "Welcome", fg = "white", bg = "red")

ckp9.7
button  = Button(window, text = "OK", fg = "white", bg = "red", command = processOK)

ckp9.8
checkbutton = Checkbutton(window, text = "apple", fg = "white", bg = "red",
                          variable = v1, command = processApple)

ckp9.9
radiobutton = Radiobutton(window, text = "senior", fg = "white", bg = "red",
                          variable = v1, command = processSenior)

ckp9.10
entry = Entry(window, fg = "white", bg = "red", variable = v1)

ckp9.11
message = Message(window, text = "programming is fun",fg = "white", bg = "red")

'''

ckp9.12

from tkinter import *

print(LEFT)
print(RIGHT)
print(CENTER)

 

 

ckp9.13-9.17

from tkinter import * # Import all definitions from tkinter

class CanvasDemo:
    def __init__(self):
        window = Tk() # Create a window
        window.title("Canvas Demo") # Set title

        # Place canvas in the window
        self.canvas = Canvas(window, width = 200, height = 200,
                             bg = "white")
        self.canvas.pack()

        # Place buttons in frame
        frame = Frame(window)
        frame.pack()
        btRectangle = Button(frame, text = "Rectangle",
                             command = self.displayRect)
        btOval = Button(frame, text = "Oval",
                        command = self.displayOval)
        btArc = Button(frame, text = "Arc",
                       command = self.displayArc)
        btPolygon = Button(frame, text = "Polygon",
                           command = self.displayPolygon)
        btLine = Button(frame, text = "Line",
                        command = self.displayLine)
        btClear = Button(frame, text = "Clear",
                         command = self.clearCanvas)
        btRectangle.grid(row = 1, column = 1)
        btOval.grid(row = 1, column = 2)
        btArc.grid(row = 1, column = 3)
        btPolygon.grid(row = 1, column = 4)
        btLine.grid(row = 1, column = 5)
        btClear.grid(row = 1, column = 7)

        window.mainloop() # Create an event loop

    # Display a rectangle
    def displayRect(self):
        self.canvas.create_rectangle(20, 20, 120, 120, fill = "red", tags="rect")

    # Display an oval
    def displayOval(self):
        self.canvas.create_oval(-30, 20, 170, 120, fill="red",
                                tags = "oval")

    # Display an arc
    def displayArc(self):
        self.canvas.create_arc(10, 10, 80, 80, start=30,
                               extent = 45, width = 2, fill = "red", tags = "arc")

    # Display a polygon
    def displayPolygon(self):
        self.canvas.create_polygon(10, 10, 15, 30, 140, 10, 10, 100,
                                   tags="polygon")

    # Display a line
    def displayLine(self):
        self.canvas.create_line(34, 50, 50, 90, fill="red",
                                tags="line")

    # Clear drawings
    def clearCanvas(self):
        self.canvas.delete("rect", "oval", "arc", "polygon",
                           "line")

CanvasDemo() # Create GUI

 

 

 

 

 

 

ckp9.18-9.20

'''
ckp9.18
The width argument can be used to specify the pen size in pixels for drawing the shapes.
设置“width”的属性值

ckp9.19
The arrow argument can be used with create_line to draw a line with an arrowhead.
The arrowhead can appear at the start, end, or both ends of the line with the
argment value first, end, or both.
设置“arrow”的属性值

ckp9.20
The activefill argument makes the shape change color when you move the mouse over
it.
设置“activefill”的属性值

'''

list9.1

from tkinter import * # Import all definitions from tkinter

window = Tk() # Create a window
label = Label(window, text = "Welcome to Python") # Create a label
button = Button(window, text = "Click Me") # Create a button
label.pack() # Place the label in the window
button.pack() # Place the button in the window

window.mainloop() # Create an event loop

 

 

list9.2

from tkinter import * # Import all definitions from tkinter

def processOK():
    print("OK button is clicked")

def processCancel():
    print("Cancel button is clicked")

window = Tk() # Create a window
btOK = Button(window, text = "OK", fg = "red", command = processOK)
btCancel = Button(window, text = "Cancel", bg = "yellow",
                  command = processCancel)
btOK.pack() # Place the OK button in the window
btCancel.pack() # Place the Cancel button in the window

window.mainloop()# Create an event loop

 

 

list9.3

from tkinter import * # Import all definitions from tkinter

class ProcessButtonEvent:
    def __init__(self):
        window = Tk() # Create a window
        btOK = Button(window, text = "OK", fg = "red", command = self.processCancel)

        btCancel = Button(window, text = "Cancel", bg = "yellow", command = self.processCancel)

        btOK.pack()
        btCancel.pack()

        window.mainloop()

    def processOK(self):
        print("OK button is clicked")

    def processCancel(self):
        print("Cancel button is clicked")

ProcessButtonEvent()

 

 

list9.4

from tkinter import * # Import all definitions from tkinter

class WidgetsDemo:
    def __init__(self):
        window = Tk() # Create a window
        window.title("Widgets Demo") # Set a title

        # Add a check button, and a radio button to frame1
        frame1 = Frame(window) # Create and add a frame to window
        frame1.pack()
        self.v1 = IntVar()
        cbtBold = Checkbutton(frame1, text = "Bold",
                              variable = self.v1, command = self.processCheckbutton)
        self.v2 = IntVar()
        rbRed = Radiobutton(frame1, text = "Red", bg = "red",
                            variable = self.v2, value = 1,
                            command = self.processRadiobutton)
        rbYellow = Radiobutton(frame1, text = "Yellow",
                               bg = "yellow", variable = self.v2, value = 2,
                               command = self.processRadiobutton)
        cbtBold.grid(row = 1, column = 1)
        rbRed.grid(row = 1, column = 2)
        rbYellow.grid(row = 1, column = 3)

        # Add a label, an entry, a button, and a message to frame1
        frame2 = Frame(window) # Create and add a frame to window
        frame2.pack()
        label = Label(frame2, text = "Enter your name: ")
        self.name = StringVar()
        entryName = Entry(frame2, textvariable = self.name)
        btGetName = Button(frame2, text = "Get Name",
                           command = self.processButton)
        message = Message(frame2, text = "It is a widgets demo")
        label.grid(row = 1, column = 1)
        entryName.grid(row = 1, column = 2)
        btGetName.grid(row = 1, column = 3)
        message.grid(row = 1, column = 4)

        # Add text
        text = Text(window) # Create and add text to the window
        text.pack()
        text.insert(END,
                    "Tip\nThe best way to learn Tkinter is to read ")
        text.insert(END,
                    "these carefully designed examples and use them ")
        text.insert(END, "to create your applications.")

        window.mainloop() # Create an event loop

    def processCheckbutton(self):
        print("check button is "
              + ("checked " if self.v1.get() == 1 else "unchecked"))

    def processRadiobutton(self):
        print(("Red" if self.v2.get() == 1 else "Yellow")
              + " is selected " )

    def processButton(self):
        print("Your name is " + self.name.get())

WidgetsDemo() # Create GUI

 

 

list9.5

from tkinter import * # Import all definitions from tkinter

class ChangeLabelDemo:
    def __init__(self):
        window = Tk() # Create a window
        window.title("Change Label Demo") # Set a title

        # Add a label to frame1
        frame1 = Frame(window) # Create and add a frame to window
        frame1.pack()
        self.lbl = Label(frame1, text = "Programming is fun")
        self.lbl.pack()

        # Add a label, entry, button, two radio buttons to frame2
        frame2 = Frame(window) # Create and add a frame to window
        frame2.pack()
        label = Label(frame2, text = "Enter text: ")
        self.msg = StringVar()
        entry = Entry(frame2, textvariable = self.msg)
        btChangeText = Button(frame2, text = "Change Text",
                              command = self.processButton)
        self.v1 = StringVar()
        rbRed = Radiobutton(frame2, text = "Red", bg = "red",
                            variable = self.v1, value = 'R',
                            command = self.processRadiobutton)
        rbYellow = Radiobutton(frame2, text = "Yellow",
                               bg = "yellow", variable = self.v1, value = 'Y',
                               command = self.processRadiobutton)

        label.grid(row = 1, column = 1)
        entry.grid(row = 1, column = 2)
        btChangeText.grid(row = 1, column = 3)
        rbRed.grid(row = 1, column = 4)
        rbYellow.grid(row = 1, column = 5)

        window.mainloop() # Create an event loop

    def processRadiobutton(self):
        if self.v1.get() == 'R':
            self.lbl["fg"] = "red"
        elif self.v1.get() == 'Y':
            self.lbl["fg"] = "yellow"

    def processButton(self):
        self.lbl["text"] = self.msg.get()# New text for the label

ChangeLabelDemo() # Create GUI

 

 

list9.6

from tkinter import * # Import all definitions from tkinter

class CanvasDemo:
    def __init__(self):
        window = Tk() # Create a window
        window.title("Canvas Demo") # Set title

        # Place canvas in the window
        self.canvas = Canvas(window, width = 200, height = 100,
                             bg = "white")
        self.canvas.pack()

        # Place buttons in frame
        frame = Frame(window)
        frame.pack()
        btRectangle = Button(frame, text = "Rectangle",
                             command = self.displayRect)
        btOval = Button(frame, text = "Oval",
                        command = self.displayOval)
        btArc = Button(frame, text = "Arc",
                       command = self.displayArc)
        btPolygon = Button(frame, text = "Polygon",
                           command = self.displayPolygon)
        btLine = Button(frame, text = "Line",
                        command = self.displayLine)
        btString = Button(frame, text = "String",
                          command = self.displayString)
        btClear = Button(frame, text = "Clear",
                         command = self.clearCanvas)
        btRectangle.grid(row = 1, column = 1)
        btOval.grid(row = 1, column = 2)
        btArc.grid(row = 1, column = 3)
        btPolygon.grid(row = 1, column = 4)
        btLine.grid(row = 1, column = 5)
        btString.grid(row = 1, column = 6)
        btClear.grid(row = 1, column = 7)

        window.mainloop() # Create an event loop

    # Display a rectangle
    def displayRect(self):
        self.canvas.create_rectangle(10, 10, 190, 90, tags="rect")

    # Display an oval
    def displayOval(self):
        self.canvas.create_oval(10, 10, 190, 90, fill="red",
                                tags = "oval")

    # Display an arc
    def displayArc(self):
        self.canvas.create_arc(10, 10, 190, 90, start=0,
                               extent = 90, width = 8, fill = "red", tags = "arc")

    # Display a polygon
    def displayPolygon(self):
        self.canvas.create_polygon(10, 10, 190, 90, 30, 50,
                                   tags="polygon")

    # Display a line
    def displayLine(self):
        self.canvas.create_line(10, 10, 190, 90, fill="red",
                                tags="line")
        self.canvas.create_line(10, 90, 190, 10, width = 9,
                                arrow = "last", activefill = "blue", tags = "line")

    # Display a string
    def displayString(self):
        self.canvas.create_text(60, 40, text="Hi, I am a string",
                                font = "Times 10 bold underline", tags = "string")

    # Clear drawings
    def clearCanvas(self):
        self.canvas.delete("rect", "oval", "arc", "polygon",
                           "line", "string")

CanvasDemo() # Create GUI

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值