利用Python的TKinter实现全屏切换

此篇博客介绍了如何使用Python的Tkinter库实现一个具有背景图片的GUI应用,通过键盘控制全屏和缩放功能。作者展示了如何加载图片、设置窗口属性,并绑定了键盘事件来切换屏幕模式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

直接上代码,包括背景图。

from tkinter import *
import tkinter as tk
from PIL import ImageTk
from PIL import Image

def get_img(filename, width, height):
    im = Image.open(filename).resize((width, height))
    im = ImageTk.PhotoImage(im)
    return im
def printkey(event):
    print('你按下了: ' + event.char)
    if event.char=='q':#按键盘q
       print('你按下了: ' + event.char)
       root.attributes("-fullscreen", False)
       mainloop()
    if event.char=='f':#按键盘f
       print('你按下了: ' + event.char)
       #root.attributes('-zoomed', True)
       root.attributes("-fullscreen", True)
       w = root.winfo_screenwidth()
       h = root.winfo_screenheight()
       #root.geometry("%dx%d" %(w,h))
       #root.attributes("-topmost",True)
       mainloop()
def main():
    '''
    注意: 背景图片im_root/im_root1这些,必须得是在主界面函数里进行加载,换个地方不行了,不知道为啥
    '''
    root.attributes("-fullscreen", True)  # substitute `Tk` for whatever your `Tk()` object is called
    root.title("背景图展示")
    root.geometry('400x600+180+100')
    #root.resizable(False, False)
    # 设置背景图片
    canvas_root = tk.Canvas(root, width=400, height=600)
    im_root = get_img('./bg2.jpeg', 400, 600)
    canvas_root.create_image(0,0, anchor='nw', image=im_root)
    
    canvas_root.pack()

    # label 中设置图片
    #im_root1 = get_img('./bg1.jpeg', 100, 40)
    #img_label = Label(root, text='欢迎使用J波检测', image=im_root1)
    #img_label.place(x=3, y=3, width=100, height=40)
    
    #实例化一个输入框
    #entry = Entry(root)
    #给输入框绑定按键监听事件<Key>为监听任何按键 <Key-x>监听其它键盘,如大写的A<Key-A>、回车<Key-Return>
    #entry.bind('<Key>', printkey)
    #显示窗体
    #entry.pack()
    root.bind('<Key>', printkey)    
    mainloop()

if __name__ == '__main__':
   root = Tk()
   main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zeroty

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值