直接上代码,包括背景图。
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()