python
主页 > 脚本 > python >

python使用tkinter与messagebox写界面和弹窗

2022-03-20 | 秩名 | 点击:

1、grid 布局

说明:

参数说明:

注意:pack() 和  grid() 是不能同时使用的 

2、Button 按钮

参数说明:

3、使用:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

from tkinter import *

from tkinter import messagebox  # python3.0的messagebox,属于tkinter的一个组件

 

top = Tk()  #。生成窗口

top.title("grid test")  #  窗口标题

top.geometry('300x400')  #。窗口大小

 

 

def box():

    return messagebox.askyesno(title='弹窗', message='内容')

 

 

Popup1 = Button(top, text="按钮1", fg="blue", bd=2, width=5, command=box, state="normal")

Popup1.grid(row=1, column=1, sticky='E')

 

Popup2 = Button(top, text="按钮2", fg="yellow", bd=2, width=5, command=box, state="normal")

Popup2.grid(row=2, column=2, sticky='NE')

效果:

4、无限循环的小弹窗:

:不选yes,不给通过!

这里需要改一下messagebox内的源代码!

1

2

3

4

    if s == CANCEL or s == NO:

        return None

    elif s == YES:

        return YES

代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

from tkinter import *

from tkinter import messagebox

 

 

tk = Tk()

tk.title('测试')

tk.geometry('100x200')

nub = 1

 

 

def Popup1():

    global nub

    '''

    askyesnocancel 弹窗:  方法解释是这样的

    Ask a question; return true if the answer is yes, None if cancelled.

    '''

    d = messagebox.askyesnocancel(title='问题', message='python \n你是否愿意继续学习下去?')

    while True:

        if d is None:

            n = Popup2(nub)

            if n is None:

                pass

            else:

                messagebox.showinfo(title=' 提示 ', message='坚持就是胜利!\n加油!一起继续学习下去!')

                #  关闭弹窗

                tk.destroy()

                return

        else:

            return

        nub += 1

 

 

def Popup2(value):

    return messagebox.askyesnocancel(title='选择', message='你选择的第%s次' % value)

 

 

d = Button(tk, text='开始选择', fg='blue', bd=2, width=10, command=Popup1)

d.grid(row=1, column=1, sticky='NE')

 

 

tk.mainloop()

原文链接:https://blog.csdn.net/weixin_44750991/article/details/120294614
相关文章
最新更新