欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中使用Tkinter模塊創(chuàng)建GUI程序?qū)嵗?/h1>
 更新時間:2015年01月14日 09:21:16   投稿:junjie  
這篇文章主要介紹了Python中使用Tkinter模塊創(chuàng)建GUI程序?qū)嵗?本文給出了創(chuà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()

相關文章

  • python sqlite3 判斷cursor的結果是否為空的案例

    python sqlite3 判斷cursor的結果是否為空的案例

    這篇文章主要介紹了python sqlite3 判斷cursor的結果是否為空的案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • 淺談django的render函數(shù)的參數(shù)問題

    淺談django的render函數(shù)的參數(shù)問題

    今天小編就為大家分享一篇淺談django的render函數(shù)的參數(shù)問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • 詳解python編程slice與indices函數(shù)用法示例

    詳解python編程slice與indices函數(shù)用法示例

    這篇文章主要介紹了詳解python編程中slice與indices使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2021-09-09
  • Python輸入整數(shù)進行排序方式

    Python輸入整數(shù)進行排序方式

    這篇文章主要介紹了Python輸入整數(shù)進行排序方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Python用函數(shù)思想完成哥德巴赫猜想代碼分析

    Python用函數(shù)思想完成哥德巴赫猜想代碼分析

    在本篇文章里小編給大家整理的是一篇關于Python用函數(shù)思想完成哥德巴赫猜想代碼分析,有興趣的朋友們可以參考下。
    2021-06-06
  • python基礎之//、/與%的區(qū)別詳解

    python基礎之//、/與%的區(qū)別詳解

    在學習Python或者使用Python進行工作的時候,大家應該都看到過“/”和“//”,下面這篇文章主要給大家介紹了關于python基礎之//、/與%區(qū)別的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-06-06
  • 使用Python實現(xiàn)遺傳算法的詳細步驟

    使用Python實現(xiàn)遺傳算法的詳細步驟

    遺傳算法是模仿自然界生物進化機制發(fā)展起來的隨機全局搜索和優(yōu)化方法,它借鑒了達爾文的進化論和孟德爾的遺傳學說,其本質(zhì)是一種高效、并行、全局搜索的方法,本文給大家介紹了使用Python實現(xiàn)遺傳算法的詳細步驟,需要的朋友可以參考下
    2023-11-11
  • 用pickle存儲Python的原生對象方法

    用pickle存儲Python的原生對象方法

    下面小編就為大家?guī)硪黄胮ickle存儲Python的原生對象方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • Python圖像處理庫PIL的ImageDraw模塊介紹詳解

    Python圖像處理庫PIL的ImageDraw模塊介紹詳解

    這篇文章主要介紹了Python圖像處理庫PIL的ImageDraw模塊介紹詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-02-02
  • Python基于os.environ從windows獲取環(huán)境變量

    Python基于os.environ從windows獲取環(huán)境變量

    這篇文章主要介紹了Python基于os.environ從windows獲取環(huán)境變量,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-06-06

最新評論