Python中使用Tkinter模塊創(chuàng)建GUI程序?qū)嵗?/div>
來源:易賢網(wǎng) 閱讀:2405 次 日期:2015-01-16 14:23:23
溫馨提示:易賢網(wǎng)小編為您整理了“Python中使用Tkinter模塊創(chuàng)建GUI程序?qū)嵗?方便廣大網(wǎng)友查閱!

使用Tkinter模塊來創(chuàng)建簡單的GUI程序。

Tkinter的Widgets有:Button、Canvas、Checkbutton、Entry、Frame、Label、Listbox、Menu、Menubutton、Message、Radiobutton、Scales、Scrollbar、TEXT、Toplevel等。

例:

代碼如下:

# This program displays an empty window.

import Tkinter

def main():

main_window = Tkinter.Tk()

Tkinter.mainloop()

main()

例2:

代碼如下:

import Tkinter

class MyGUI:

def __init__(self):

# Create the main window widget.

self.main_window = Tkinter.Tk()

# Enter the Tkinter main loop.

Tkinter.mainloop()

# Create an instance of the MyGUI class.

my_gui = MyGUI()

例3:

代碼如下:

# The program displays a label with text.

import Tkinter

class MyGUI:

def __init__(self):

self.main_window = Tkinter.Tk()

# Create a Label widget containing the text 'Hello world'

self.label = Tkinter.Label(self.main_window, text='Hello World!')

# Call the Label widget's pack method.

self.label.pack()

# Enter the Tkinter main loop.

Tkinter.mainloop()

# Create an instance of the MyGUI class.

my_gui = MyGUI()

例4:

代碼如下:

import Tkinter

class MyGUI:

def __init__(self):

self.main_window = Tkinter.Tk()

self.label1 = Tkinter.Label(self.main_window,text='Hello World!')

self.label2 = Tkinter.Label(self.main_window,text='This is my GUI program.')

self.label1.pack()

self.label2.pack()

Tkinter.mainloop()

mygui = MyGUI()

例5:

代碼如下:

import Tkinter

class MyGUI:

def __init__(self):

self.main_window = Tkinter.Tk()

self.label1 = Tkinter.Label(self.main_window,text='Hello World!')

self.label2 = Tkinter.Label(self.main_window,text='This is my GUI program.')

self.label1.pack(side='left')

self.label2.pack(side='left')

Tkinter.mainloop()

mygui = MyGUI()

例6:

代碼如下:

import Tkinter

class MyGUI:

def __init__(self):

self.main_window = Tkinter.Tk()

self.top_frame = Tkinter.Frame(self.main_window)

self.bottom_frame = Tkinter.Frame(self.main_window)

self.label1 = Tkinter.Label(self.top_frame,text='Winken')

self.label2 = Tkinter.Label(self.top_frame,text='Blinken')

self.label3 = Tkinter.Label(self.top_frame,text='Nod')

self.label1.pack(side='top')

self.label2.pack(side='top')

self.label3.pack(side='top')

self.label4 = Tkinter.Label(self.bottom_frame,text='Winken')

self.label5 = Tkinter.Label(self.bottom_frame,text='Blinken')

self.label6 = Tkinter.Label(self.bottom_frame,text='Nod')

self.label4.pack(side='left')

self.label5.pack(side='left')

self.label6.pack(side='left')

self.top_frame.pack()

self.bottom_frame.pack()

Tkinter.mainloop()

mygui = MyGUI()

按鈕Widget和信息對話框

使用tkMessageBox模塊的showinfo函數(shù)來顯示信息對話框。

例:

代碼如下:

# the program demonstrates a Button widget.

# when the user clicks the button, an info dialog box is displayed.

import Tkinter

import tkMessageBox

class MyGUI:

def __init__(self):

self.main_window = Tkinter.Tk()

self.my_button = Tkinter.Button(self.main_window, text='Click me!',command=self.do_something)

self.my_button.pack()

Tkinter.mainloop()

def do_something(self):

tkMessageBox.showinfo('Response','Thanks for clicking the button.')

mygui = MyGUI()

例2:

代碼如下:

import Tkinter

import tkMessageBox

class MyGUI:

def __init__(self):

self.main_window = Tkinter.Tk()

self.my_button = Tkinter.Button(self.main_window, text='Click me!',command=self.do_something)

self.quit_button = Tkinter.Button(self.main_window,text='Quit',command=self.main_window.quit)

self.my_button.pack()

self.quit_button.pack()

Tkinter.mainloop()

def do_something(self):

tkMessageBox.showinfo('Response','Thanks for clicking the button.')

mygui = MyGUI()

用Entry Widget得到輸入

Entry Widget是一個矩形區(qū)域,用戶可輸入其中??墒褂肊ntry Widget的get方法取回輸入的數(shù)據(jù)。

例:

代碼如下:

import Tkinter

import tkMessageBox

class KiloGUI:

def __init__(self):

self.main_window = Tkinter.Tk()

self.top_frame = Tkinter.Frame(self.main_window)

self.bottom_frame = Tkinter.Frame(self.main_window)

self.label = Tkinter.Label(self.top_frame,text='Enter a distance in kilometers:')

self.entry = Tkinter.Entry(self.top_frame,width=10)

self.button1 = Tkinter.Button(self.bottom_frame,text='Convert',command=self.convert)

self.button2 = Tkinter.Button(self.bottom_frame,text='Quit',command=self.main_window.quit)

self.label.pack(side='left')

self.entry.pack(side='left')

self.button1.pack(side='left')

self.button2.pack(side='left')

self.top_frame.pack()

self.bottom_frame.pack()

Tkinter.mainloop()

def convert(self):

kilo = float(self.entry.get())

miles = kilo*0.6214

tkMessageBox.showinfo('Result',str(kilo)+' kilometers is equal to '+str(miles)+' miles.')

mygui = KiloGUI()

例2:

代碼如下:

import Tkinter

import tkMessageBox

class KiloGUI:

def __init__(self):

self.main_window = Tkinter.Tk()

self.top_frame = Tkinter.Frame(self.main_window)

self.mid_frame = Tkinter.Frame(self.main_window)

self.bottom_frame = Tkinter.Frame(self.main_window)

self.label1 = Tkinter.Label(self.top_frame,text='Enter a distance in kilometers:')

self.entry = Tkinter.Entry(self.top_frame,width=10)

self.button1 = Tkinter.Button(self.bottom_frame,text='Convert',command=self.convert)

self.button2 = Tkinter.Button(self.bottom_frame,text='Quit',command=self.main_window.quit)

self.label2 = Tkinter.Label(self.mid_frame,text='Converted to miles:')

self.value = Tkinter.StringVar()

self.label3 = Tkinter.Label(self.mid_frame,textvariable=self.value)

self.label1.pack(side='left')

self.entry.pack(side='left')

self.button1.pack(side='left')

self.button2.pack(side='left')

self.label2.pack(side='left')

self.label3.pack(side='left')

self.top_frame.pack()

self.mid_frame.pack()

self.bottom_frame.pack()

Tkinter.mainloop()

def convert(self):

kilo = float(self.entry.get())

miles = kilo*0.6214

self.value.set(miles)

mygui = KiloGUI()

Radio按鈕和Check按鈕

例:

代碼如下:

import Tkinter

import tkMessageBox

class MyGUI:

def __init__(self):

self.main_window = Tkinter.Tk()

self.top_frame = Tkinter.Frame(self.main_window)

self.bottom_frame = Tkinter.Frame(self.main_window)

self.radio_var = Tkinter.IntVar()

self.radio_var.set(1)

self.rb1 = Tkinter.Radiobutton(self.top_frame,text='Option 1',variable=self.radio_var,value=1)

self.rb2 = Tkinter.Radiobutton(self.top_frame,text='Option 2',variable=self.radio_var,value=2)

self.rb3 = Tkinter.Radiobutton(self.top_frame,text='Option 3',variable=self.radio_var,value=3)

self.rb1.pack()

self.rb2.pack()

self.rb3.pack()

self.ok_button = Tkinter.Button(self.bottom_frame,text='OK',command=self.show_choice)

self.quit_button = Tkinter.Button(self.bottom_frame,text='QUIT',command=self.main_window.quit)

self.ok_button.pack(side='left')

self.quit_button.pack(side='left')

self.top_frame.pack()

self.bottom_frame.pack()

Tkinter.mainloop()

def show_choice(self):

tkMessageBox.showinfo('Selection','You selected optioin '+str(self.radio_var.get()))

mygui = MyGUI()

更多信息請查看IT技術(shù)專欄

更多信息請查看腳本欄目
易賢網(wǎng)手機(jī)網(wǎng)站地址:Python中使用Tkinter模塊創(chuàng)建GUI程序?qū)嵗?/a>
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機(jī)號
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)