Tkinter |使用 ttk.Entry 小部件為輸入的文本添加樣式

登錄小部件 用於從用戶那裡檢索條目。可以這樣創建:

 entry = ttk.Entry (master, option = value, ...) 

Code # 1: 創建一個小部件條目和獲取用戶輸入(僅獲取字符串數據)。

# tkinter 導入

from tkinter import *

from tkinter import ttk

from tkinter.messagebox import askyesno


# 創建根

root = Tk()


#指定幾何圖形

< p> root.geometry ( ’200x100’ )


# 這是用來獲取用戶輸入的
# 並在 Entry Widget 中顯示。
# 無論我們從鍵盤接收到什麼數據
# 都將被視為一個字符串。

input_text = StringVar() 代碼>

entry1 = ttk.Entry (root, textvariable = input_text, justify = CENTER)


#focus_force 用於焦點
# 應用程序一啟動
entry1.focus_force ()

entry1.pack (side = TOP, ipadx = 30 , ipady = 6 )

save = ttk.Button (root, text = ’保存’ , 命令 = lambda : askyesno (

’確認’ , ’要保存嗎? ’ ))

save.pack (side = TOP, pady = 10 )


root.mainloop ()

輸出:

在上面的輸出中,一旦你運行代碼中, 按鈕, 會出現一條確認消息,詢問您是否要保存文本(文本不會被保存,它是只用過顯示按鈕的功能)。

代碼#2: 為輸入小部件中輸入的文本添加樣式。

< td class = "code">

# tkinter import

from tkinter import *

from tkinter import ttk

from tkinter.messagebox import askyesno


# 創建根目錄

root = tk()

root.geometry ( ’ 200x100’ )

input_text = StringVar()


# 這個類是用來添加樣式的
# 到任何可用的小部件

style = ttk.Style ( )

style.configure ( ’TEntry’ ,foreground = ’green’ )

entry1 = ttk. entry (root, textvariable = input_text, justify = CENTER,

字體 = ( ’courier’ , 15 , ’bold’ ))

entry1.focus_force()

entry1.pack (side = TOP, ipadx = 30 , ipady = 10 )

保存 = ttk.按鈕(根,文本 = ’Save’ ,命令 = lambda : askyesno (

’確認’ , ’ 你要保存嗎?’ ))

save.pack (side = TOP, pady = 10 )


root.mainloop ()

輸出:

在上面的輸出中,你可能注意到字體顏色發生了變化,字體系列發生了變化,文字是 la rger 比正常,並且文本是粗體。這是因為我們正在為我們輸入的文本添加樣式。

我們希望本文能幫助您解決問題。 除了 Tkinter |使用 ttk.Entry 小部件為輸入的文本添加樣式,查看其他與 Python functions 相關的主題。

想在 Python 中出類拔萃? 查看我們對 最佳 Python 在線課程 2023的評論。 如果您對數據科學感興趣,請查看如何學習 R 編程

順便說一句,此材料也有其他語言版本:



Jan Innsbruck

Rome | 2023-03-30

谢谢你的解释! 我被Tkinter |使用 ttk.Entry 小部件為輸入的文本添加樣式卡住了几个小时。了几个小时,最后终于完成了 🤗. 昨天檢查了,有效!

Angelo Krasiko

Vigrinia | 2023-03-30

谢谢你的解释! 我被Tkinter |使用 ttk.Entry 小部件為輸入的文本添加樣式卡住了几个小时。了几个小时,最后终于完成了 🤗. 明天會回來反饋

Davies Robinson

Vigrinia | 2023-03-30

简单地说,很清楚。谢谢你的分享。Tkinter |使用 ttk.Entry 小部件為輸入的文本添加樣式和其他问题的Python functions一直是我的弱点😁。. 明天會回來反饋

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically