溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

在python項目中如何使用 tkinter模塊

發布時間:2020-11-09 15:23:01 來源:億速云 閱讀:265 作者:Leah 欄目:開發技術

在python項目中如何使用 tkinter模塊?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

tkinter提供了三個模塊,可以創建彈出對話窗口:(使用必須單獨導入模塊)

1.messagebox  消息對話框

  示例:askokcancel

在python項目中如何使用 tkinter模塊

import tkinter
# 導入消息對話框子模塊
import tkinter.messagebox

# 創建主窗口
root = tkinter.Tk()
# 設置窗口大小
root.minsize(300,300)

# 聲明函數
def okqqq():
  # 彈出對話框
  result = tkinter.messagebox.askokcancel(title = '標題~',message='內容:要吃飯嘛?')  # 返回值為True或者False
  print(result)
# 添加按鈕
btn1 = tkinter.Button(root,text = 'ok',command = okqqq)
btn1.pack()

# 加入消息循環
root.mainloop()

  示例:askquestion

在python項目中如何使用 tkinter模塊

import tkinter
# 導入消息對話框子模塊
import tkinter.messagebox

# 創建主窗口
root = tkinter.Tk()
# 設置窗口大小
root.minsize(300,300)

# 聲明函數
def question():
  # 彈出對話框
  result = tkinter.messagebox.askquestion(title = '標題',message='內容:你吃飯了嘛?')
  # 返回值為:yes/no
  print(result)
# 添加按鈕
btn1 = tkinter.Button(root,text = 'question',command = question)
btn1.pack()

# 加入消息循環
root.mainloop()

  示例:askretrycancel  (重試)

在python項目中如何使用 tkinter模塊

import tkinter
# 導入消息對話框子模塊
import tkinter.messagebox

# 創建主窗口
root = tkinter.Tk()
# 設置窗口大小
root.minsize(300,300)
# 聲明函數
def retry():
  # 彈出對話框
  result = tkinter.messagebox.askretrycancel(title = '標題',message='內容:女生拒絕了你???')
  # 返回值為:True或者False
  print(result)
# 添加按鈕
btn1 = tkinter.Button(root,text = 'retry',command = retry)
btn1.pack()
# 加入消息循環
root.mainloop()

  示例:askyesno

在python項目中如何使用 tkinter模塊

# 聲明函數
def yesno():
  # 彈出對話框
  result = tkinter.messagebox.askyesno(title = '標題',message='內容:你喜歡我嗎?')
  # 返回值為:True或者False
  print(result)
# 添加按鈕
btn1 = tkinter.Button(root,text = 'yesno',command = yesno)
btn1.pack()

  示例:showerror (出錯)

在python項目中如何使用 tkinter模塊

# 聲明函數
def error():
  # 彈出對話框
  result = tkinter.messagebox.showerror(title = '出錯了!',message='內容:你的年齡不符合要求。')
  # 返回值為:ok
  print(result)
# 添加按鈕
btn1 = tkinter.Button(root,text = 'error',command = error)
btn1.pack()

  示例:showwarning(警告)

在python項目中如何使用 tkinter模塊

# 聲明函數
def warning():
  # 彈出對話框
  result = tkinter.messagebox.showwarning(title = '出錯了!',message='內容:十八歲以下禁止進入。')
  # 返回值為:ok
  print(result)
# 添加按鈕
btn1 = tkinter.Button(root,text = 'warning',command = warning)
btn1.pack()

  示例:showinto (信息提示)

在python項目中如何使用 tkinter模塊

# 聲明函數
def info():
  # 彈出對話框
  result = tkinter.messagebox.showinfo(title = '信息提示!',message='內容:您的女朋友收到一只不明來歷的口紅!')
  # 返回值為:ok
  print(result)
# 添加按鈕
btn1 = tkinter.Button(root,text = 'info',command = info)
btn1.pack()

2.simpledialog  簡單信息對話框

   示例:asksting(獲取字符串)

在python項目中如何使用 tkinter模塊

import tkinter
# 導入子模塊
import tkinter.simpledialog

# 創建主窗口
root = tkinter.Tk()
# 設置窗口大小
root.minsize(300,300)

# 創建函數
def askname():
  # 獲取字符串(標題,提示,初始值)
  result = tkinter.simpledialog.askstring(title = '獲取信息',prompt='請輸入姓名:',initialvalue = '可以設置初始值')
  # 打印內容
  print(result)
# 添加按鈕
btn = tkinter.Button(root,text = '獲取用戶名',command = askname)
btn.pack()

# 加入消息循環
root.mainloop()

  示例:askinteger(獲取整型)

在python項目中如何使用 tkinter模塊

import tkinter
# 導入消息對話框子模塊
import tkinter.simpledialog

# 創建主窗口
root = tkinter.Tk()
# 設置窗口大小
root.minsize(300,300)

# 創建函數
def askage():
  # 獲取整型(標題,提示,初始值)
  result = tkinter.simpledialog.askinteger(title = '獲取信息',prompt='請輸入年齡:',initialvalue = '18')
  # 打印內容
  print(result)
# 添加按鈕
btn = tkinter.Button(root,text = '獲取年齡',command = askage)
btn.pack()

# 加入消息循環
root.mainloop()

  示例:askfloat(獲取浮點型)

在python項目中如何使用 tkinter模塊

import tkinter
# 導入消息對話框子模塊
import tkinter.simpledialog

# 創建主窗口
root = tkinter.Tk()
# 設置窗口大小
root.minsize(300,300)

# 創建函數
def askheight():
  # 獲取浮點型數據(標題,提示,初始值)
  result = tkinter.simpledialog.askfloat(title = '獲取信息',prompt='請輸入身高(單位:米):',initialvalue = '18.0')
  # 打印內容
  print(result)
# 添加按鈕
btn = tkinter.Button(root,text = '獲取身高',command = askheight)
btn.pack()

# 加入消息循環
root.mainloop()

關于在python項目中如何使用 tkinter模塊問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女