Python tkinter之Entry


1.Entry的基本屬性

 
         
# -*- encoding=utf-8 -*-

import tkinter
from tkinter import *

if __name__ == '__main__':
win = tkinter.Tk() # 窗口
win.title('南風丶輕語') # 標題
screenwidth = win.winfo_screenwidth() # 屏幕寬度
screenheight = win.winfo_screenheight() # 屏幕高度
width = 500
height = 300
x = int((screenwidth - width) / 2)
y = int((screenheight - height) / 2)
win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置

entry = Entry(
master=win, # 父容器
text='標簽', # 文本
bg='pink', # 背景顏色
fg='red', # 文本顏色
relief='sunken', # 邊框的3D樣式 flat、sunken、raised、groove、ridge、solid。
bd=3, # 邊框的大小
width=10, # 寬度
state='normal', # 設置狀態 normal、readonly、 disabled
cursor='arrow', # 鼠標移動時樣式 arrow, circle, cross, plus...
font=('黑體', 20), # 字體
show='*', # 顯示的字符
)
entry.pack()
win.mainloop()

 備注:

①支持的字體(通過tkinter.font.families獲取)https://www.cnblogs.com/rainbow-tan/p/14043822.html/

②鼠標樣式選項

"arrow", "circle", "clock", "cross", "dotbox", "exchange", "fleur", "heart", "man", "mouse", "pirate", "plus","shuttle", "sizing", "spider", "spraycan", "star","target", "tcross", "trek", "watch"

 2、邊框樣式,組件狀態閱覽

import tkinter
from tkinter import *


def event():
    print('點擊事件')


if __name__ == '__main__':
    win = tkinter.Tk()  # 窗口
    win.title('南風丶輕語')  # 標題
    screenwidth = win.winfo_screenwidth()  # 屏幕寬度
    screenheight = win.winfo_screenheight()  # 屏幕高度
    width = 980
    height = 300
    x = int((screenwidth - width) / 2)
    y = int((screenheight - height) / 2)
    win.geometry('{}x{}+{}+{}'.format(width, height, x, y))  # 大小以及位置

    values = ['flat', 'sunken', 'raised', 'groove', 'ridge', 'solid']
    for index, value in enumerate(values):
        entry = Entry(
                master=win,  # 父容器
                text=value,  # 文本
                bg='yellow',  # 背景顏色
                fg='red',  # 文本顏色
                relief=value,  # 邊框的3D樣式 flat、sunken、raised、groove、ridge、solid。
                bd=5,  # 邊框的大小
                width=10,  # 寬度
                state='normal',  # 設置狀態 normal、readonly、 disabled
                cursor='arrow',  # 鼠標移動時樣式 arrow, circle, cross, plus...
                font=('Yu Gothic Medium', 15),  # 字體
                )
        entry.grid(row=0, column=index, padx=10, pady=10)
        entry.insert('end', value)  # 插入字符串

    values = ['normal', 'readonly', 'disabled']
    for index, value in enumerate(values):
        entry = Entry(
                master=win,  # 父容器
                text=value,  # 文本
                bg='yellow',  # 背景顏色
                fg='red',  # 文本顏色
                relief='raised',  # 邊框的3D樣式 flat、sunken、raised、groove、ridge、solid。
                bd=3,  # 邊框的大小
                width=10,  # 寬度
                state=value,  # 設置狀態 normal、readonly、 disabled
                cursor='arrow',  # 鼠標移動時樣式 arrow, circle, cross, plus...
                font=('Yu Gothic Medium', 15),  # 字體
                )
        entry.grid(row=1, column=index, padx=10, pady=10)
        entry.insert('end', value)  # 插入字符串

    win.mainloop()

 3.插入字符串

 
         
# -*- encoding=utf-8 -*-

import tkinter
from tkinter import *


def end_insert():
entry.insert('end', 'A') # 插入到末尾


def point_insert():
entry.insert('insert', 'B') # 插入到光標處


def start_insert():
entry.insert(0, 'C') # 插入到開頭


def four_insert():
entry.insert(4, 'D') # 插入到指定下標


def get():
msg = entry.get() # 獲取輸入的信息
print(msg)


if __name__ == '__main__':
win = tkinter.Tk() # 窗口
win.title('南風丶輕語') # 標題
screenwidth = win.winfo_screenwidth() # 屏幕寬度
screenheight = win.winfo_screenheight() # 屏幕高度
width = 500
height = 300
x = int((screenwidth - width) / 2)
y = int((screenheight - height) / 2)
win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置

entry = Entry(
master=win, # 父容器
text='標簽', # 文本
bg='pink', # 背景顏色
fg='red', # 文本顏色
relief='sunken', # 邊框的3D樣式 flat、sunken、raised、groove、ridge、solid。
bd=3, # 邊框的大小
width=10, # 寬度
state='normal', # 設置狀態 normal、readonly、 disabled
cursor='arrow', # 鼠標移動時樣式 arrow, circle, cross, plus...
font=('黑體', 20), # 字體
show='', # 顯示的字符
)
entry.pack()
Button(text='插入到末尾', command=end_insert).pack()
Button(text='插入到鼠標位置', command=point_insert).pack()
Button(text='插入到開始', command=start_insert).pack()
Button(text='插入到下標4處', command=four_insert).pack()
Button(text='獲取輸入的信息', command=get).pack()
win.mainloop()
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM