比较详细具体的教程:
Python tkinter教程-04:输入框_罗富茗的博客-CSDN博客_python tkinter 输入框
字体设置:
tkinter模块字体(font)的使用 | Python笔记
import tkinter as tk
import tkinter.font as tkFont
root = tk.Tk() # must be here
f1 = tkFont.Font(family='microsoft yahei', size=16, weight='bold')
f2 = tkFont.Font(family='times', size=20, slant='italic')
f3 = tkFont.Font(family='Helvetica', size=24, underline=1, overstrike=1)
tk.Label(root, text='tkFont', font=f1).pack()
tk.Label(root, text='tkFont', font=f2).pack()
tk.Label(root, text='tkFont', font=f3).pack()
tk.Label(root, text='default font').pack()
print(tkFont.families())
root.mainloop()
窗口大小及位置设置:
Python tkinter(笔记)设置Tk窗口放置在屏幕中央_ZQHCSD的博客-CSDN博客
两个输入框,输入符合要求的值后点击按键退出
#以上均为教程。以下要输入层数
import tkinter as tk
import tkinter.font as tkFont
from tkinter import StringVar
import time
window = tk.Tk()
#字体设置,注意中文字体需为ubunt支持的中文字体,具体可由tkFont.families()输出查看
f1 = tkFont.Font(family='song ti', size=20, weight='bold')
f2 = tkFont.Font(family='song ti', size=16, weight='bold')
f3 = tkFont.Font(family='gothic', size=16)
f4 = tkFont.Font(family='song ti', size=16)
#窗口大小
width = 500
heigh = 200
#获取屏幕尺寸
screenwidth = window.winfo_screenwidth()
screenheight = window.winfo_screenheight()
window.title("dicom裁剪范围")
window.geometry('%dx%d+%d+%d'%(width, heigh, (screenwidth-width)/2, (screenheight-heigh)/2))
l0 = tk.Label(window, text="请设置需裁剪的dicom切片范围(至少包含48张切片)",font=f1)
l0.pack()
l1 = tk.Label(window, text="起始裁剪层数:",font=f2)
l1.pack()
e1 = tk.Entry(window, font=f3)
e1.pack()
l2 = tk.Label(window, text="结束裁剪层数:", font=f2)
l2.pack()
e2 = tk.Entry(window, font=f3)
e2.pack()
# def get():
# print("将裁剪以下切片:",e1.get(),":",e2.get())
# b = tk.Button(window, text="完成设置!", font=f2, command=get)
# b.pack()
def ending():
global start_slice,end_slice,text
start_slice=e1.get()
end_slice=e2.get()
text = StringVar()
# print(start_slice,end_slice)
# print(type(start_slice))
# print(type(end_slice))
try:
start_slice = int(start_slice.strip())
end_slice = int(end_slice.strip())
# print(start_slice,end_slice)
# print(type(start_slice))
# print(type(end_slice))
if (end_slice-start_slice)<48 or(start_slice<0):#关于end_slice超出上限的部分在后面代码强制限定
text.set("输入错误,请重新输入!")
err = tk.Label(window, textvariable=text, font=f4, fg="red")
err.pack()
else:
text.set("设置成功!")
err = tk.Label(window, textvariable=text, font=f4, fg="red")
err.pack()
time.sleep(3)
window.destroy()
except:
text.set("输入错误,请重新输入!")
err = tk.Label(window, textvariable=text, font=f4, fg="red")
err.pack()
# tk.Button(window,text='退出',width=15, pady=5, command=ending).place(x=300, y=150, anchor='nw')
tk.Button(window,text='完成',width=15, pady=5, command=ending).pack(side='right')
window.mainloop()
print(start_slice,end_slice)