|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
from tkinter import *
root = Tk()
root.title("居中的窗口")
screenWidth = root.winfo_screenwidth() # 获取显示区域的宽度
screenHeight = root.winfo_screenheight() # 获取显示区域的高度
width = 600 # 设定窗口宽度
height = 400 # 设定窗口高度
left = (screenWidth - width) / 2
top = (screenHeight - height) / 2
# 宽度x高度+x偏移+y偏移
# 在设定宽度和高度的基础上指定窗口相对于屏幕左上角的偏移位置
root.geometry("%dx%d+%d+%d" % (width, height, left, top))
def love():
master = Tk()
master.title("居中的窗口")
screenWidth = master.winfo_screenwidth()
screenHeight = master.winfo_screenheight()
width = 300
height = 160
left = (screenWidth - width) / 2
top = (screenHeight - height) / 2
master.geometry("%dx%d+%d+%d" % (width, height, left, top))
l=Label(master,text='是的呢')
l.pack()
mainloop()
def dislove():
master = Tk()
frame1=Frame()
photo = PhotoImage(file=r"E:\P.py\课堂练习.py\tkinter\眼瞎.png")
w = Label(frame1, image=photo)
w.pack()
frame1.pack()
while True:
textlable = Label(root,text='祢豆子真漂亮')
textlable.pack()
photo = PhotoImage(file=r"E:\P.py\课堂练习.py\tkinter\1.png")
w = Label(root, image=photo)
w.pack()
button1=Button(root,text='喜欢',command=love)
button2=Button(root,text='不喜欢',command=dislove)
button1.pack()
button2.pack()
mainloop()
dislove里的image为啥显示不出来???运行的结果在图片上,求大佬指点
知道为什么了,你图片在全局作用域打开即可,在局部打开一瞬间图片就会被垃圾回收机制给回收了
把这里的代码:
- def dislove():
- master = Toplevel()
- disphoto = PhotoImage(file=r"E:\P.py\课堂练习.py\tkinter\眼瞎.png") # 这边这个移动到 你while 循环里
- w = Label(master, image=photo)
- w.pack()
复制代码
移到这即可显示图片:
- while True:
- textlable = Label(root, text='祢豆子真漂亮')
- textlable.pack()
- disphoto = PhotoImage(file=r"E:\P.py\课堂练习.py\tkinter\眼瞎.png")
- photo = PhotoImage(file=r"E:\P.py\课堂练习.py\tkinter\1.png")
复制代码
|
-
|