xia的小窩

一起來coding和碼字吧

0%

文字方塊

實做文字框框

簡單的格子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from tkinter import *


root = Tk()
root.title("python-tkinter")
root.geometry("300x400")
root.configure(bg = "lightblue")

nameL = Label(root, text = "Name =>", bg = "lightblue")
nameL.grid(row = 0)

addressL = Label(root, text = "Address => ", bg = "lightblue")
addressL.grid(row = 1)


nameE = Entry(root)
addressE = Entry(root)
nameE.grid(row = 0, column = 1)
addressE.grid(row = 1, column = 1)

root.mainloop()

結果

使用show參數隱藏字元

使用show參數

1
addressE = Entry(root, show = "*")

結果

Enrty的get( )方法

嘗試使用get函數

1
2
3
4
5
6
def printmsg():
print(nameE.get(), addressE.get())

loginbtn = Button(root, text = "login", command = printmsg)
loginbtn.grid(row = 3, column = 0)

結果

Entry的insert( )方法

為預設內容

1
2
3
4
5
6
7
# 省略
nameE = Entry(root)
addressE = Entry(root, show = "*")
nameE.insert(0, "Rex")
nameE.grid(row = 0, column = 1)
addressE.grid(row = 1, column = 1)
# 省略

結果

Enrty的delete( )方法

加在函數裡面

1
2
3
4
def printmsg():
print(nameE.get(), addressE.get())
nameE.delete(0, END)
addressE.delete(0, END)

結果

使用eval表達式

輸入型式的計算機 :point_down:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from tkinter import *

def cal():
out.configure(text = "結果: " + str(eval(equ.get())))

root = Tk()
root.title("python-tkinter")
root.geometry("600x600")
root.configure(bg = "lightblue")

label = Label(root, text = "請輸入數學表達式: ", bg = "lightblue")
label.pack()

equ = Entry(root)
equ.pack(pady = 5)

out = Label(root, bg = "lightblue")
out.pack()

btn = Button(root, text = "計算", command = cal)
btn.pack(pady = 5)

root.mainloop()

結果