使用Tkinter模組來建立簡單的GUI程式
學了一段時間python,總想用它來做一個gui的介面程式,嘿嘿,主要是為了炫耀一下啊。下面整理了一些python Tkinter模組的小例子,希望大家能夠做一個gui程式出來。
使用Tkinter模組來建立簡單的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函式來顯示資訊對話方塊。
例:
# 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()
相關文章
- Python如何使用tkinter編寫GUI程式PythonGUI
- Python Tkinter 簡單使用Python
- Paramiko模組簡單使用
- python inspect模組簡單使用Python
- python中的itertools模組簡單使用Python
- python的GUI程式設計和tkinter學習筆記——第一個GUI程式PythonGUI程式設計筆記
- Python GUI程式設計:tkinter關於ttkbootstrapPythonGUI程式設計boot
- 簡單聊聊模組
- PyQt5 GUI程式設計(QMainWindow與QWidget模組結合使用)QTGUI程式設計AI
- python爬蟲:爬蟲的簡單介紹及requests模組的簡單使用Python爬蟲
- 在Java 11中建立一個簡單的模組化應用教程Java
- python-GUI之tkinter的學習PythonGUI
- Python tkinter是什麼?GUI程式設計有哪些?PythonGUI程式設計
- 使用Leaflet建立地圖模組地圖
- Tkinter (40) ttk 模組所有部件的通用方法
- 建立一個簡單的小程式
- python3中的re模組簡單介紹及使用Python
- 使用 Go 模組建立專案(vgo)Go
- 對於JS模組的簡單瞭解JS
- 前端模組化簡單總結前端
- 簡單介紹python process模組Python
- 使用 Raku 編寫簡單的文字識別模擬程式
- Rust GUI庫 egui 的簡單應用RustGUI
- Nuxt Kit 的使用指南:模組建立與管理UX
- 透過程式碼例項簡單瞭解Python sys模組Python
- 從module的簡單實現到模組化
- Zmail-簡單易用的python郵件模組AIPython
- 簡單聊一聊Javascript中的模組化JavaScript
- [譯]使用JavaScript建立WebAssembly模組例項JavaScriptWeb
- 進擊的模組化+webpack的簡單實現Web
- 使用策略模式和簡單工廠模式重寫支付模組模式
- Spring【DAO模組】就是這麼簡單Spring
- Spring【AOP模組】就是這麼簡單Spring
- Tkinter系列教程01—引言和安裝Tk—Python GUI程式設計PythonGUI程式設計
- 使用Python建立簡單的HTTP伺服器PythonHTTP伺服器
- Python tkinter 實現 指令碼工具 GUI模版Python指令碼GUI
- tkinter中表格的建立(十三)
- python的tkinter程式設計(四)GUI介面裡面使用類進行開發,也就是自定義元件Python程式設計GUI元件
- 使用簡單的Java程式碼在SAP C4C裡建立銷售訂單Java