文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>python scripts GUI design

python scripts GUI design

时间:2007-03-25  来源:blackjimmy

把学习的关于gui设计的代码贴上吧。
这些代码都是在FC6环境下,用kate写的。我感觉kate比较好用。呵呵
今天的代码贴到这里,不语法加亮了,奇怪!可能是没有下载flash插件吧。
不知道你们看着有没有颜色!呵呵

1,click_counter.py
from Tkinter import *

class Application(Frame):
    " " " GUI application which counts button clicks." " "
    def __init__(self, master):
        " " " Initialize the frame." " "
        Frame.__init__(self, master)
        self.grid()
        self.bttn_clicks=0         # creates an object atribute to keep track of the number of
                                                       # times the user clicks the button
        self.create_widget()
    def create_widget(self):
        " " " Create button which displays number of clicks. " " "
        self.bttn=Button(self)
        self.bttn["text"]="total clicks: 0"
        self.bttn["command"]=self.update_count
        self.bttn.grid()
        # in general , you set a widget's command option to
        # bind the activation of the widget with an event handler
    def update_count(self):
        " " " Increase click count and display new total." " "
        self.bttn_clicks+=1
        self.bttn["text"]="total clicks:" + str(self.bttn_clicks)
        # this method increments the total number of button clicks
        # and then change the text of the button to reflect the new total
   
# wrapping up the program
# main
root=Tk()
root.title("click counter")
root.geometry("200x100")

app=Application(root)
root.mainloop()


2,labeler.py
from Tkinter import *

# create the root window
root=Tk()
root.title("labeler")
root.geometry("200x50")

# create a frame in the window to hold other widgets
app=Frame(root)
# the new frame is placed inside the root window
app.grid()
# let you arrange widgets

# create a label in the frame
lb1=Label(app, text="I'm a label!")
# app is the place where "i'm a label!" exists
lb1.grid()

# kick off the window's event loop
root.mainloop()

3,lazy_buttons2.py
from Tkinter import *

class Application(Frame):
    " " " A GUI application with three buttons." " "
    # an application object is just a specialized type of Frame object
   
    # define Application's constructor
    def __init__(self, master):
        " " " Initialize the Frame " " "
        Frame.__init__(self, master)
        self.grid()
        self.create_widgets()
   
    def create_widgets(self):
        " " " Create three buttons that do nothing." " "
        # create first botton
        self.bttn1=Button(self, text="i do nothing")
        self.bttn1.grid()
       
        # create second button
        self.bttn2=Button(self)
        self.bttn2.grid()
        self.bttn2.configure(text="me too")
       
        # create third button
        self.bttn3=Button(self)
        self.bttn3.grid()
        self.bttn3["text"]="same here"
# main       
root=Tk()
root.title("lazy button 2")
root.geometry("200x100")

app=Application(root)
# this code creates a Application object with the root window as its master
# the Application object's constructor invokes the object's create_widgets() methods
# this method then create the three buttons
# with the Application object as their master

root.mainloop()


4,lazy_buttons.py

from Tkinter import *

# create a root window
root=Tk()
root.title("lazy button")
root.geometry("200x100")
# hint: you must not use "=" here

# create a frame in the window to hold other widgets ( the buttons here)
app=Frame(root)
app.grid()

# create a button in the frame
bttn1=Button(app, text="I do nothing")
bttn1.grid()

bttn2=Button(app)
bttn2.grid()
# all i've done is add a blank button to the frame
bttn2.configure(text="Me too")
# you can use a widget's configure() method for any widgets option

# create a third button in the frame
bttn3=Button(app)
bttn3.grid()
bttn3["text"]="same here"
# i access the button's text option through a dictionary-like interface
# I set the text option to the string "same here", which puts the text
# "same here" on the buttons

# kick off the window's event loop
root.mainloop()

5,movie_chooser2.py

from Tkinter import *
class Application(Frame):
    " " " GUI Application from favorite movie type." " "
    def __init__(self, master):
        " " "  Initialize Frame. " " "
        Frame.__init__(self, master)
        self.grid()
        self.create_widgets()
    def create_widgets(self):
        " " " Create widgets for movie type choices. " " "
        # create description label
        Label(self,
             text="choose your favorite type of movie"
            ).grid(row=0, column=0, sticky=W)
        # create instruction label
        Label(self,
             text="select one:"
             ).grid(row=1, column=0, sticky=W)
       
        # create variable for single, favorite type of movie
        self.favorite=StringVar()
       
        # create comedy radio button
        Radiobutton(self,
                    text="comedy",
                    variable=self.favorite,
                    value="comedy",
                    command=self.update_text
                    ).grid(row=2, column=0, sticky=W)
        # create drama radio button
        Radiobutton(self,
                    text="drama",
                    variable=self.favorite,
                    value="drama",
                    command=self.update_text
                    ).grid(row=3, column=0, sticky=W)
        # create romance radio buttons
        Radiobutton(self,
                    text="romance",
                    variable=self.favorite,
                    value="romance",
                    command=self.update_text
                    ).grid(row=4, column=0, sticky=W)
        # create text field to display results
        self.results_txt=Text(self, width=40, height=5, wrap=WORD)
        self.results_txt.grid(row=5, column=0, columnspan=3)
       
    def update_text(self):
        " " " Update text widget and display user's favorite movie types." " "
        message="your favorite type of movie is "
        message+=self.favorite.get()

        self.results_txt.delete(0.0, END)
        self.results_txt.insert(0.0, message)
       
# main
root=Tk()
root.title("movie chooser 2")
app=Application(root)
root.mainloop()
6movie_chooser.py

from Tkinter import *
class Application(Frame):
    " " " GUI Application from favorite movie types." " "
    def __init__(self, master):
        Frame.__init__(self, master)
        self.grid()
        self.create_widgets()
    def create_widgets(self):
        " " " Create widgets for movie type choices. " " "
        # create description label
        Label(self,
             text="choose your favorite movie types"
            ).grid(row=0, column=0, sticky=W)
        # create instruction label
        Label(self,
             text="select all that apply:"
             ).grid(row=1, column=0, sticky=W)
       
        # create comedy check button
        self.likes_comedy=BooleanVar()
        Checkbutton(self, text="comedy",
                    variable=self.likes_comedy,
                    command=self.update_text
                    ).grid(row=2, column=0, sticky=W)
        # create drama check button
        self.likes_drama=BooleanVar()
        Checkbutton(self,
                    text="drama",
                    variable=self.likes_drama,
                    command=self.update_text
                    ).grid(row=3, column=0, sticky=W)
        # create romance check buttons
        self.likes_romance=BooleanVar()
        Checkbutton(self,
                    text="romance",
                    variable=self.likes_romance,
                    command=self.update_text
                    ).grid(row=4, column=0, sticky=W)
        # create text field to display results
        self.results_txt=Text(self, width=40, height=5, wrap=WORD)
        self.results_txt.grid(row=5, column=0, columnspan=3)
    def update_text(self):
        " " " Update text widget and display user's favorite movie types." " "
        likes = " "
       
        if self.likes_comedy.get():
            likes+="you like comedic movies.\n"
        if self.likes_drama.get():
            likes+="you like dramatic movies.\n"
        if self.likes_romance.get():
            likes+="you  like romantic movies."
        self.results_txt.delete(0.0, END)
        self.results_txt.insert(0.0, likes)
       
# main
root=Tk()
root.title("movie chooser")
app=Application(root)
root.mainloop()
7,simple_gui.py
from Tkinter import *

# create the root window
root=Tk()
# i don't have to prefix the module name Tkinter
# I can directly access any part of the Tkinter module
# you can have only one root window in a Tkinter program

# modifying the window
root.title("simple GUI")
# set the title of the root window
root.geometry("800x600")
# set the size of the root window,
# "x" character, between 'z' and 'c' on the keyboard

# kick off the window's event loop
root.mainloop()

# it is a full-fledged window than can resize, minimize, and close
8,
longevity.py

from Tkinter import *
class Application(Frame):
    " " " GUI application which can reveal the secret of longevity. " " "
    def __init__(self, master):
        " " " Initialize the frame. " " "
        Frame.__init__(self, master)
        self.grid()
        self.create_widgets()
    def create_widgets(self):
        " " " Create button, text, and entry widgets. " " "
        # create instruction label
        self.inst_la1=Label(self, text="enter password for the secret of longevity")
        self.inst_la1.grid(row=0, column=0, columnspan=2, sticky=W)
       
        # create label for password
        self.pw_lb1=Label(self, text="password:")
        self.pw_lb1.grid(row=1, column=0, sticky=W)
        # create entry widgets to accept password
        self.pw_ent=Entry(self)
        # this code creates a text entry where the user can enter a password
        self.pw_ent.grid(row=1, column=1, sticky=W)
        # create submit button
        self.submit_bttn=Button(self, text="submit", command=self.reveal)
        # i bind the activation of the button with the reveal() method
        # which reveals the longevity secret
        self.submit_bttn.grid(row=2, column=0, sticky=W)
       
        # create text widgets to display message
        self.secret_txt=Text(self, width=35, height=5, wrap=WORD)
        self.secret_txt.grid(row =3, column=0, columnspan=2, sticky=W)
   
    def reveal(self):
        " " " Display message based on password. " " "
        contents=self.pw_ent.get()
        # the get() method returns the text in the widgets
        if contents=="secret":
            message="here's the secret to living to 100: live to 99 and then be VERY careful."
        else:
            message="that's not the correct password, so i can't share the secret with you ."
        self.secret_txt.delete(0.0 , END)
        # the delete() method can delete text from text-based widgets
        self.secret_txt.insert(0.0 , message)
        # the insert() method can insert a text-based widgets

# main
root=Tk()
root.title("Longevity")
root.geometry("250x150")

app=Application(root)
root.mainloop()


要是看着不舒服的话,请下载原文件:
文件: 24.tar.gz
大小: 2KB
下载: 下载
相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载