最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python中使用Tkinter模塊創(chuàng)建GUI程序?qū)嵗?/h1>
 更新時(shí)間:2015年01月14日 09:21:16   投稿:junjie  
這篇文章主要介紹了Python中使用Tkinter模塊創(chuàng)建GUI程序?qū)嵗?本文給出了創(chuàng)建窗口、文本框、按鈕等實(shí)例,需要的朋友可以參考下

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

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

例:

復(fù)制代碼 代碼如下:

# This program displays an empty window.
import Tkinter
def main():
  main_window = Tkinter.Tk()
  Tkinter.mainloop()
main()

例2:
復(fù)制代碼 代碼如下:

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:
復(fù)制代碼 代碼如下:

# 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:
復(fù)制代碼 代碼如下:

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:
復(fù)制代碼 代碼如下:

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:
復(fù)制代碼 代碼如下:

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和信息對(duì)話框
使用tkMessageBox模塊的showinfo函數(shù)來顯示信息對(duì)話框。
例:
復(fù)制代碼 代碼如下:

# 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:
復(fù)制代碼 代碼如下:

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是一個(gè)矩形區(qū)域,用戶可輸入其中。可使用Entry Widget的get方法取回輸入的數(shù)據(jù)。
例:
復(fù)制代碼 代碼如下:

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:
復(fù)制代碼 代碼如下:

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按鈕
例:
復(fù)制代碼 代碼如下:

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()

相關(guān)文章

  • pandas數(shù)據(jù)清洗,排序,索引設(shè)置,數(shù)據(jù)選取方法

    pandas數(shù)據(jù)清洗,排序,索引設(shè)置,數(shù)據(jù)選取方法

    下面小編就為大家分享一篇pandas數(shù)據(jù)清洗,排序,索引設(shè)置,數(shù)據(jù)選取方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • 使用python實(shí)現(xiàn)ftp的文件讀寫方法

    使用python實(shí)現(xiàn)ftp的文件讀寫方法

    今天小編就為大家分享一篇使用python實(shí)現(xiàn)ftp的文件讀寫方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • Python獲取網(wǎng)絡(luò)圖片和視頻的示例代碼

    Python獲取網(wǎng)絡(luò)圖片和視頻的示例代碼

    Python 是一種多用途語言,廣泛用于腳本編寫。我們可以編寫Python 腳本來自動(dòng)化日常事務(wù)。本文將用Python實(shí)現(xiàn)獲取Google圖片和YouTube視頻,需要的可以參考一下
    2022-03-03
  • Python的Geopy庫處理地理編碼與位置信息

    Python的Geopy庫處理地理編碼與位置信息

    地理編碼和位置信息在現(xiàn)代應(yīng)用中扮演著重要角色,本文主要介紹了Python的Geopy庫處理地理編碼與位置信息,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-12-12
  • OpenCV?imread讀取圖片失敗的問題及解決

    OpenCV?imread讀取圖片失敗的問題及解決

    這篇文章主要介紹了OpenCV?imread讀取圖片失敗的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Python?os和os.path模塊詳情

    Python?os和os.path模塊詳情

    這篇文章主要介紹了Python?os和os.path模塊詳情,文章圍繞主題展開詳細(xì)的相關(guān)內(nèi)容,具有一定的參考價(jià)值,需要的小伙伴可以參考一下,希望對(duì)你的學(xué)習(xí)有所幫助
    2022-04-04
  • np.random.seed() 的使用詳解

    np.random.seed() 的使用詳解

    這篇文章主要介紹了np.random.seed() 的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • python sklearn包——混淆矩陣、分類報(bào)告等自動(dòng)生成方式

    python sklearn包——混淆矩陣、分類報(bào)告等自動(dòng)生成方式

    今天小編就為大家分享一篇python sklearn包——混淆矩陣、分類報(bào)告等自動(dòng)生成方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Python and、or以及and-or語法總結(jié)

    Python and、or以及and-or語法總結(jié)

    這篇文章主要介紹了Python and、or以及and-or語法總結(jié),本文分別給出實(shí)例講解它們的使用方法,需要的朋友可以參考下
    2015-04-04
  • Python 多維List創(chuàng)建的問題小結(jié)

    Python 多維List創(chuàng)建的問題小結(jié)

    這篇文章主要介紹了Python 多維List創(chuàng)建的問題小結(jié),詳細(xì)的介紹了遇到的一個(gè)小問題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01

最新評(píng)論

沂源县| 开远市| 徐水县| 阜新市| 双城市| 泌阳县| 东乡| 黎平县| 榆中县| 株洲县| 景东| 宁津县| 水富县| 体育| 通渭县| 东阳市| 当阳市| 张家界市| 靖宇县| 承德县| 恭城| 宕昌县| 娄底市| 虞城县| 休宁县| 密山市| 灵寿县| 平武县| 灯塔市| 犍为县| 霸州市| 乳山市| 西吉县| 长葛市| 闸北区| 延川县| 德兴市| 民乐县| 浦城县| 囊谦县| 大洼县|