鱼C论坛

 找回密码
 立即注册
查看: 2455|回复: 20

[已解决]button

[复制链接]
发表于 2020-5-19 23:46:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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 = Toplevel()

    master.title("居中的窗口")
    screenWidth = master.winfo_screenwidth()  
    screenHeight = master.winfo_screenheight()  
    width = 600  
    height = 500
    left = (screenWidth - width) / 2
    top = (screenHeight - height) / 2
    master.geometry("%dx%d+%d+%d" % (width, height, left, top))
    w = Label(master, image=lphoto)
    w.pack()
    Button(master,text='确定',command=master.quit).pack()
    mainloop()   


def dislove():
    master = Toplevel()
#    disphoto = PhotoImage(file=r"E:\P.py\课堂练习.py\tkinter\眼瞎.png")
   
#    textlabel=Label(master,text='你怕是眼瞎').pack()
   

    master.title("让你认清自己!!!")
    screenWidth = master.winfo_screenwidth()  
    screenHeight = master.winfo_screenheight()  
    width = 600
    height = 380
    left = (screenWidth - width) / 2
    top = (screenHeight - height) / 2
    master.geometry("%dx%d+%d+%d" % (width, height, left, top))
    w = Label(master, image=disphoto)
    w.pack()
    Button(master,text='确定',command=master.quit).pack()
    mainloop()


textlable = Label(root,text='祢豆子真漂亮')
textlable.pack()

lphoto = PhotoImage(file=r"E:\P.py\课堂练习.py\tkinter\喜欢.png")
disphoto = PhotoImage(file=r"E:\P.py\课堂练习.py\tkinter\眼瞎.png")
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()
为啥love和第三love的窗口需要点击两下button才能生效
最佳答案
2020-5-19 23:54:31
这样即可:
  1. from tkinter import *

  2. root = Tk()

  3. root.title("居中的窗口")
  4. screenWidth = root.winfo_screenwidth()  # 获取显示区域的宽度
  5. screenHeight = root.winfo_screenheight()  # 获取显示区域的高度
  6. width = 600  # 设定窗口宽度
  7. height = 400  # 设定窗口高度
  8. left = (screenWidth - width) / 2
  9. top = (screenHeight - height) / 2

  10. # 宽度x高度+x偏移+y偏移
  11. # 在设定宽度和高度的基础上指定窗口相对于屏幕左上角的偏移位置
  12. root.geometry("%dx%d+%d+%d" % (width, height, left, top))


  13. def love():
  14.     master = Toplevel()

  15.     master.title("居中的窗口")
  16.     screenWidth = master.winfo_screenwidth()
  17.     screenHeight = master.winfo_screenheight()
  18.     width = 600
  19.     height = 500
  20.     left = (screenWidth - width) / 2
  21.     top = (screenHeight - height) / 2
  22.     master.geometry("%dx%d+%d+%d" % (width, height, left, top))
  23.     w = Label(master, image=lphoto)
  24.     w.pack()
  25.     Button(master, text='确定', command=master.quit).pack()



  26. def dislove():
  27.     master = Toplevel()
  28.     #    disphoto = PhotoImage(file=r"E:\P.py\课堂练习.py\tkinter\眼瞎.png")

  29.     #    textlabel=Label(master,text='你怕是眼瞎').pack()

  30.     master.title("让你认清自己!!!")
  31.     screenWidth = master.winfo_screenwidth()
  32.     screenHeight = master.winfo_screenheight()
  33.     width = 600
  34.     height = 380
  35.     left = (screenWidth - width) / 2
  36.     top = (screenHeight - height) / 2
  37.     master.geometry("%dx%d+%d+%d" % (width, height, left, top))
  38.     w = Label(master, image=disphoto)
  39.     w.pack()
  40.     Button(master, text='确定', command=master.quit).pack()
  41.    


  42. textlable = Label(root, text='祢豆子真漂亮')
  43. textlable.pack()

  44. lphoto = PhotoImage(file=r"1.png")
  45. disphoto = PhotoImage(file=r"1.png")
  46. photo = PhotoImage(file=r"1.png")
  47. w = Label(root, image=photo)
  48. w.pack()

  49. button1 = Button(root, text='喜欢', command=love)
  50. button2 = Button(root, text='不喜欢', command=dislove)

  51. button1.pack()
  52. button2.pack()

  53. mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-5-19 23:53:54 | 显示全部楼层
因为你用了两个 mainloop()
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-19 23:54:31 | 显示全部楼层    本楼为最佳答案   
这样即可:
  1. from tkinter import *

  2. root = Tk()

  3. root.title("居中的窗口")
  4. screenWidth = root.winfo_screenwidth()  # 获取显示区域的宽度
  5. screenHeight = root.winfo_screenheight()  # 获取显示区域的高度
  6. width = 600  # 设定窗口宽度
  7. height = 400  # 设定窗口高度
  8. left = (screenWidth - width) / 2
  9. top = (screenHeight - height) / 2

  10. # 宽度x高度+x偏移+y偏移
  11. # 在设定宽度和高度的基础上指定窗口相对于屏幕左上角的偏移位置
  12. root.geometry("%dx%d+%d+%d" % (width, height, left, top))


  13. def love():
  14.     master = Toplevel()

  15.     master.title("居中的窗口")
  16.     screenWidth = master.winfo_screenwidth()
  17.     screenHeight = master.winfo_screenheight()
  18.     width = 600
  19.     height = 500
  20.     left = (screenWidth - width) / 2
  21.     top = (screenHeight - height) / 2
  22.     master.geometry("%dx%d+%d+%d" % (width, height, left, top))
  23.     w = Label(master, image=lphoto)
  24.     w.pack()
  25.     Button(master, text='确定', command=master.quit).pack()



  26. def dislove():
  27.     master = Toplevel()
  28.     #    disphoto = PhotoImage(file=r"E:\P.py\课堂练习.py\tkinter\眼瞎.png")

  29.     #    textlabel=Label(master,text='你怕是眼瞎').pack()

  30.     master.title("让你认清自己!!!")
  31.     screenWidth = master.winfo_screenwidth()
  32.     screenHeight = master.winfo_screenheight()
  33.     width = 600
  34.     height = 380
  35.     left = (screenWidth - width) / 2
  36.     top = (screenHeight - height) / 2
  37.     master.geometry("%dx%d+%d+%d" % (width, height, left, top))
  38.     w = Label(master, image=disphoto)
  39.     w.pack()
  40.     Button(master, text='确定', command=master.quit).pack()
  41.    


  42. textlable = Label(root, text='祢豆子真漂亮')
  43. textlable.pack()

  44. lphoto = PhotoImage(file=r"1.png")
  45. disphoto = PhotoImage(file=r"1.png")
  46. photo = PhotoImage(file=r"1.png")
  47. w = Label(root, image=photo)
  48. w.pack()

  49. button1 = Button(root, text='喜欢', command=love)
  50. button2 = Button(root, text='不喜欢', command=dislove)

  51. button1.pack()
  52. button2.pack()

  53. mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-19 23:55:20 | 显示全部楼层
Twilight6 发表于 2020-5-19 23:53
因为你用了两个 mainloop()

mainloop() 只留最后一个即可
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-20 09:41:57 | 显示全部楼层
Twilight6 发表于 2020-5-19 23:55
mainloop() 只留最后一个即可

ok,我试试
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-20 09:44:42 | 显示全部楼层

对了  忘记告诉你  弥豆子恋柱 都是我老婆
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-20 09:53:54 | 显示全部楼层
Twilight6 发表于 2020-5-20 09:44
对了  忘记告诉你  弥豆子 和 恋柱 都是我老婆

???我有点蒙,大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-20 09:54:28 | 显示全部楼层
Twilight6 发表于 2020-5-20 09:44
对了  忘记告诉你  弥豆子 和 恋柱 都是我老婆

这种带隐藏的怎么打出来???
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-20 09:55:02 | 显示全部楼层
Twilight6 发表于 2020-5-20 09:44
对了  忘记告诉你  弥豆子 和 恋柱 都是我老婆

咱们各论各的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-20 09:55:42 | 显示全部楼层
君子好逑 发表于 2020-5-20 09:54
这种带隐藏的怎么打出来???

把字体颜色变白即可

刮刮卡:你好像也是福建的?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-20 09:56:26 | 显示全部楼层

用鼠标刮开可以看得见字
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-20 09:56:49 | 显示全部楼层
Twilight6 发表于 2020-5-20 09:55
把字体颜色变白即可

刮刮卡:你好像也是福建的?

不懂,嘿嘿嘿,一会下课我试试
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-20 09:59:05 | 显示全部楼层
君子好逑 发表于 2020-5-20 09:56
不懂,嘿嘿嘿,一会下课我试试

刮刮卡.gif

Button 问题应该已经解决了,记得给个最佳哈哈

文字颜色,和字体背景颜色设置同种即可
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-20 10:48:58 | 显示全部楼层

大佬,我想让程序循环起来,有什么好的方法吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-20 10:56:55 | 显示全部楼层
君子好逑 发表于 2020-5-20 10:48
大佬,我想让程序循环起来,有什么好的方法吗
  1. command=master.destroy
复制代码

不用quit   quit 是直接把根窗口退了  
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-20 11:00:12 | 显示全部楼层
Twilight6 发表于 2020-5-20 10:56
不用quit   quit 是直接把根窗口退了

这是什么原理???为什么root窗口一直没退???
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-20 11:02:22 | 显示全部楼层
君子好逑 发表于 2020-5-20 11:00
这是什么原理???为什么root窗口一直没退???

destroy是隐藏或者销毁组件的意思,在你代码中销毁的组件是顶层窗口组件,你在root重新点击就重新生成顶层窗口,你点确定又销毁
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-20 11:07:22 | 显示全部楼层
Twilight6 发表于 2020-5-20 11:02
destroy是隐藏或者销毁组件的意思,在你代码中销毁的组件是顶层窗口组件,你在root重新点击就重新生成顶 ...

懂了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-20 11:09:34 | 显示全部楼层


欢迎继续发帖提问~~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-20 11:10:34 | 显示全部楼层
Twilight6 发表于 2020-5-20 11:09
欢迎继续发帖提问~~

必须啊,作为菜逼选手就只能请求场外援助啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-30 04:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表