n_rand
=
randint
(
s
,
n
)
print
(
n_rand
)
a_lbv
.
set
(
"已设置好一个%d-%d范围内的随机数"
%
(
s
,
n
)
)
s_Lab
.
set
(
"猜一个%d-%d范围内的随机数"
%
(
s
,
n
)
)
except
:
a_lbv
.
set
(
"请正确设置"
)
root
=
tk
.
Tk
(
)
root
.
title
(
"猜大小"
)
root
.
geometry
(
"300x160+500+200"
)
root
.
resizable
(
0
,
0
)
note
=
ttk
.
Notebook
(
root
)
note
.
pack
(
fill
=
"both"
,
expand
=
True
)
n_rand
=
randint
(
0
,
100
)
frm1
=
tk
.
Frame
(
note
)
ss
=
"猜一个整数(默认0-100之间)"
s_Lab
=
tk
.
StringVar
(
)
s_Lab
.
set
(
ss
)
s
=
tk
.
Label
(
frm1
,
bg
=
'red'
,
fg
=
'yellow'
,
width
=
25
,
textvariable
=
s_Lab
)
s
.
pack
(
side
=
"left"
,
padx
=
5
,
pady
=
5
,
fill
=
"both"
)
num_E
=
tk
.
StringVar
(
)
e
=
tk
.
Entry
(
frm1
,
width
=
10
,
textvariable
=
num_E
)
e
.
pack
(
padx
=
5
,
pady
=
30
)
e
.
focus_set
(
)
r_btn
=
tk
.
Button
(
frm1
,
text
=
'确定'
,
width
=
10
,
command
=
lambda
:
hit
(
num_E
,
n_rand
)
)
r_btn
.
pack
(
padx
=
5
)
root
.
bind
(
'<Return>'
,
key_re
)
frm1
.
pack
(
fill
=
"both"
,
expand
=
True
)
note
.
add
(
frm1
,
text
=
"猜数字"
)
frm2
=
tk
.
Frame
(
note
,
padx
=
20
,
pady
=
20
)
a_lbv
=
tk
.
StringVar
(
)
a_lbv
.
set
(
"设置起始与结束值,初始随机数为0-100"
)
a_lb
=
tk
.
Label
(
frm2
,
textvariable
=
a_lbv
)
.
pack
(
pady
=
10
)
s_n
=
tk
.
IntVar
(
)
s_E1
=
tk
.
Entry
(
frm2
,
width
=
10
,
textvariable
=
s_n
)
.
pack
(
side
=
'left'
,
padx
=
10
,
pady
=
5
)
z_lb
=
tk
.
Label
(
frm2
,
text
=
"至"
)
.
pack
(
side
=
'left'
,
pady
=
5
)
e_n
=
tk
.
IntVar
(
)
e_n
.
set
(
100
)
e_E2
=
tk
.
Entry
(
frm2
,
width
=
10
,
textvariable
=
e_n
)
e_E2
.
pack
(
side
=
'left'
,
padx
=
10
,
pady
=
5
)
set_btn
=
tk
.
Button
(
frm2
,
width
=
10
,
text
=
"设置"
,
command
=
ran_set
)
set_btn
.
pack
(
side
=
'right'
,
padx
=
10
,
pady
=
5
)
frm2
.
pack
(
ipadx
=
50
,
ipady
=
20
)
note
.
add
(
frm2
,
text
=
"设置"
)
root
.
mainloop
(
)
利用这个小游戏可以学习一下ython3.3中
tkinter
的
使用
方法复制代码 代码如下:# -*- coding: utf-8 -*-import
tkinter
as tkimport sysimport randomimport re
number = random.randint(0,1024)running = Truenum = 0nmaxn = 1024nminn = 0
def eBtnClose(event): root.destroy()def eBtnGuess(event): global nmaxn global nminn global num
众所周知,在
Python
中创建图形界面程序有很多种的选择,其中PyQt和wx
Python
都是很热门的模块包,这些第三方的图形界面模块功能强大、配置丰富,界面美观,是很多人的选择。州的先生也经常
使用
PyQt5来为
Python
程序
写
上图形界面,以方便程序的
使用
。同时还
写
了一个简短的PyQt5入门教程:《州的先生PyQt5入门教程》,对PyQt5感兴趣的小伙伴可以前往阅读。而今天,我们要介绍的是
Python
内置的一个图形界面模块——
tkinter
。在网络上有很多抱怨
Tkinter
模块的言论,比如:界面丑、不灵活、扩展功能差、模块少……等等,诚然,
Tkinter
确实有这些毛病或者说是缺点,但是它也并
def admin_main(self):
self.admin_main_root = tk.Tk()
self.admin_main_root.geometry("850x700+{}+{}".format(
self.admin_main_root.winfo_screenwidth() // 2 - 850 // 2,
self.admin_main_root.winfo_screenheight() // 2 ...
def __init__(self,master):
self.
notebook
= ttk.
Notebook
(master)
self.frame1 = tk.Frame(master)
self.frame2
为
python
tkinter
界面创建
分页
栏。
需要
使用
Notebook
()和Frame()。
使用
Notebook
()创建
分页
栏,Frame()创建多个框架来当做不同的页面。
import matplotlib.pyplot as plt
from
tkinter
import *
import
tkinter
.ttk as ttk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
import
tkinter
.messagebox
import
tkinter
.ttk as ttk
from PIL import Image,ImageTk,ImageSequence
import time
import requests
import json
if __name_