君子好逑 发表于 2020-5-19 22:23:48

tkinter

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为啥显示不出来???运行的结果在图片上,求大佬指点

Twilight6 发表于 2020-5-19 22:29:16

因为在一个程序中只能存在一个根窗口,也就是说只能存在一个Tk(),其他的窗口只能以顶层窗口(Toplevel())的形式存在。

Twilight6 发表于 2020-5-19 22:34:25

def dislove():
    master = Toplevel()
    photo = PhotoImage(file=r"E:\P.py\课堂练习.py\tkinter\眼瞎.png")
    w = Label(master, image=photo)
    w.pack()
这样改改试试看

君子好逑 发表于 2020-5-19 22:36:31

Twilight6 发表于 2020-5-19 22:29
因为在一个程序中只能存在一个根窗口,也就是说只能存在一个Tk(),其他的窗口只能以顶层窗口(Toplevel()) ...

什么叫顶层窗口???

Twilight6 发表于 2020-5-19 22:37:27

君子好逑 发表于 2020-5-19 22:36
什么叫顶层窗口???

初略的理解为显示最前面的窗口就是顶层窗口

君子好逑 发表于 2020-5-19 22:38:04

Twilight6 发表于 2020-5-19 22:34
这样改改试试看

好像不行{:10_266:}

Twilight6 发表于 2020-5-19 22:38:24

君子好逑 发表于 2020-5-19 22:38
好像不行

报错了嘛?还是怎么了

君子好逑 发表于 2020-5-19 22:39:09

Twilight6 发表于 2020-5-19 22:34
这样改改试试看

但是如果是文字的话是可以显示的

君子好逑 发表于 2020-5-19 22:39:46

Twilight6 发表于 2020-5-19 22:38
报错了嘛?还是怎么了

倒是没报错,就是啥都不显示

君子好逑 发表于 2020-5-19 22:40:19

Twilight6 发表于 2020-5-19 22:38
报错了嘛?还是怎么了

就是窗口比我原来那个大一点

君子好逑 发表于 2020-5-19 22:43:24

Twilight6 发表于 2020-5-19 22:34
这样改改试试看

大佬,你有想这样需要在函数中产生新窗口的源程序吗,能让我观摩一波吗

Twilight6 发表于 2020-5-19 22:44:36

君子好逑 发表于 2020-5-19 22:43
大佬,你有想这样需要在函数中产生新窗口的源程序吗,能让我观摩一波吗

没怎么写Tkinter 因为也是最近刚刚学到的

君子好逑 发表于 2020-5-19 22:45:45

Twilight6 发表于 2020-5-19 22:44
没怎么写Tkinter 因为也是最近刚刚学到的

那好吧,我再去网上找找{:10_266:}

Twilight6 发表于 2020-5-19 22:46:28

君子好逑 发表于 2020-5-19 22:45
那好吧,我再去网上找找

小甲鱼课程都上完了嘛?Tkinter的

君子好逑 发表于 2020-5-19 22:48:15

Twilight6 发表于 2020-5-19 22:46
小甲鱼课程都上完了嘛?Tkinter的

嘿嘿嘿,没有,这不一哥们找我一起玩抖音的表白程序,我上到tkinter3了,寻思着能用到的组件都学了,就准备试一试{:10_256:}

Twilight6 发表于 2020-5-19 22:52:21

君子好逑 发表于 2020-5-19 22:48
嘿嘿嘿,没有,这不一哥们找我一起玩抖音的表白程序,我上到tkinter3了,寻思着能用到的组件都学了,就准 ...

知道为什么了,你图片在全局作用域打开即可,在局部打开一瞬间图片就会被垃圾回收机制给回收了
把这里的代码:
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")

Twilight6 发表于 2020-5-19 22:54:25

Twilight6 发表于 2020-5-19 22:52
知道为什么了,你图片在全局作用域打开即可,在局部打开一瞬间图片就会被垃圾回收机制给回收了
把这里的 ...

我这边改了变量名,否则变量名重复了

君子好逑 发表于 2020-5-19 23:23:48

Twilight6 发表于 2020-5-19 22:54
我这边改了变量名,否则变量名重复了

666大佬{:10_256:}

Twilight6 发表于 2020-5-19 23:26:10

君子好逑 发表于 2020-5-19 23:23
666大佬

你有两个我回答的帖子没‘结账’ 哦嘿嘿~ 兄弟你懂得~

君子好逑 发表于 2020-5-19 23:27:03

Twilight6 发表于 2020-5-19 22:54
我这边改了变量名,否则变量名重复了

终于成功的我留下了感动的泪水{:10_266:}
页: [1] 2
查看完整版本: tkinter