python圖形GUI基本樣本

來源:互聯網
上載者:User

標籤:矩形地區   var   imp   and   comm   hello   roo   文本   alt   

【注: 以下樣本python3版本】

樣本一:

import tkinter as tk

app = tk.Tk()
app.title("FishC Demo")

theLable = tk.Label(app, text="我的第二個視窗程序",width=20,height=10)#標籤控制項;可以顯示文本和位元影像
theLable.pack()

app.mainloop()

運行結果:

 

樣本二:

 import tkinter as tk

class App: #定義類
    
    def __init__(self, master):
        frame = tk.Frame(master)#定義表單架構
        frame.pack(side=tk.RIGHT, padx=100,pady=100)
        
        #定義按鈕
        self.hi_there = tk.Button(frame, text="打招呼",bg="red" ,fg=‘green‘,command=self.say_hi)
        self.hi_there.pack(padx=10, pady=10)#位置
        
        
    def say_hi(self):
        print("Hi! 大家好,終於我跟大家見面了。")
            
            
root = tk.Tk()
app = App(root)
root.mainloop()

 

運行結果:

 

 樣本三

 from tkinter import *

root = Tk()

textLabel = Label(root,text="有些心疼,又帶著欣喜。\n她那麼簡單,不好看,卻努力的開著,\
\n為了迎合清風掠心的柔情,一直努力的開著,\n半盞清歌的招搖,\
肆意蔥蘢。\n而我,恰巧路過。於是,\n喜意便盈盈的生了起來,帶\
著異樣的溫暖和樸素,\n將心,都染了濕濕的綠,盎然的,\n萬千端倪。", justify=LEFT, padx=10) #Label 標籤控制項;可以顯示文本和位元影像

textLabel.pack(side=LEFT)

photo = PhotoImage(format="png",file=r"C:\Users\Administrator\Desktop\123 (2).png")#PhotoImagecan be used for GIF and PPM/PGM color bitmaps

imgLabel = Label(root, image=photo)
imgLabel.pack(side=RIGHT)

mainloop()

 

運行結果:

文字排版不啥樣,只是展示效果而已,請原諒!

 

 

樣本四

from tkinter import *

root = Tk()

photo = PhotoImage(file=r"C:\Users\Administrator\Desktop\123.png")

theLable = Label(root,
                 text=‘Hello‘,
                 justify = LEFT,
                 image=photo,
                 compound=CENTER,
                 font=("黑體",20),
                 fg = "red")

theLable.pack()
mainloop()

 

運行結果:

 

展示效果而已!

 

樣本五

from tkinter import *

def callback():
    var.set(‘吹吧你,我才不信呢‘)
    
root = Tk()

frame1 = Frame(root)#Frame    架構控制項;在螢幕上顯示一個矩形地區,多用來作為容器
frame2 = Frame(root)

var = StringVar()  #設定字串
var.set("你所下載的影片含有末成人限制內容,\n請滿18周歲後再點擊觀看")

textLabel = Label(frame1,
                  textvariable = var,
                  justify = LEFT)

textLabel.pack(side=LEFT)

photo = PhotoImage(file=r"C:\Users\Administrator\Desktop\123 (2).png")

imgLabel = Label(frame1,text="圖片一", image=photo)
imgLabel.pack(side=RIGHT)


theButton = Button(frame2,text="我已滿18周歲", command=callback) #定義一個按鈕
theButton.pack()

frame1.pack(padx=10, pady=10)#定義位置
frame2.pack(padx=50, pady=50)

mainloop()

 

運行結果

 

 樣本六

from tkinter import *

root = Tk()

frame = Frame(root)

v = IntVar()

c = Checkbutton(root, text="測試一下", variable=v)#定義一個複選框
c.pack()

l = Label(root, textvariable=v)
l.pack()

if __name__ == ‘__main__‘:
    mainloop()

運行結果:

 

 樣本七

 

python圖形GUI基本樣本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.