【问题标题】:How to fix this Python tkinter problem with button Layout如何使用按钮布局解决此 Python tkinter 问题
【发布时间】:2020-05-05 19:56:31
【问题描述】:

我在 python 中做了一个石头剪刀布游戏,一切都很好,直到我添加了图片。看看我的代码:

player_score=0
comp_score=0
def press():
    print("You Pressed ROCK")
    ent=1
    global player_score
    global comp_score
    for i in range(0,1):
        comp_ent=(random.randint(1,3))
    if comp_ent==3:
        print("You won...Computer Choose Scissors")
        player_score+=1
    elif comp_ent==2:
        print("You Lose...Computer Choose Paper")
        comp_score+=1
    else:
        print("It's a Draw...Computer Choose Rock")
    print("Player Score:",player_score)
    print("Computer Score:",comp_score)    
    print("\n")
def press1():
    print("You Pressed PAPER")
    ent=2
    global player_score
    global comp_score
    for i in range(0,1):
        comp_ent=(random.randint(1,3))
    if comp_ent==1:
        print("You won...Computer Choose Rock")
        player_score+=1
    elif comp_ent==3:
        print("You Lose...Computer Choose Scissors")
        comp_score+=1
    else:
        print("It's a Draw...Computer Choose Paper")
    print("Player Score:",player_score)
    print("Computer Score:",comp_score)
    print("\n")
def press2():
    print("You Pressed SCISSORS")
    ent=3
    global player_score
    global comp_score    
    for i in range(0,1):
        comp_ent=(random.randint(1,3))
    if comp_ent==2:
        print("You won...Computer Choose Paper")
        player_score+=1
    elif comp_ent==1:
        print("You Lose...Computer Choose Rock")
        comp_score+=1
    else:
        print("It's a Draw...Computer Choose Scissors")
    print("Player Score:",player_score)
    print("Computer Score:",comp_score)    
    print("\n")
import random
from tkinter import *
import tkinter as tk
root = tk.Tk()
photo1=PhotoImage(file=r"C:\Users\Abhinav\Desktop\Important Folder\Python Project\rock2.gif")
photo2=PhotoImage(file=r"C:\Users\Abhinav\Desktop\Important Folder\Python Project\paper2.gif")
photo3=PhotoImage(file=r"C:\Users\Abhinav\Desktop\Important Folder\Python Project\scissors 2.gif")
frame = tk.Frame(root)
frame.pack()
button = tk.Button(root)
button.config(image=photo1,width="300",height="200",text="ROCK",fg="Black",command=press)
button.pack(side=TOP)
button2 = tk.Button(root)
button.config(image=photo2,width="300",height="200",text="PAPER",fg="Blue",command=press1)
button2.pack(side=BOTTOM)
button3 = tk.Button(root)
button3.config(image=photo3,width="300",height="200",text="SCISSORS",fg="Green",command=press2)
button3.pack(side=LEFT)
button3.config(image=photo3)
root.mainloop()

我这样做是为了我的学校项目。

Screen shot

【问题讨论】:

  • 我建议阅读以下文章:ericlippert.com/2014/03/05/how-to-debug-small-programs。这个元帖子也很相关:meta.stackoverflow.com/questions/284236/…。顺便说一句,我认为程序的设计可以改进,尤其是在全局变量的使用方面。
  • 好吧,没用。但是您收到任何错误消息了吗?
  • 不,它只显示为屏幕截图......当我点击小按钮时也没有输出......
  • 请不要为了跳过问题过滤器而添加复制+粘贴华夫饼。
  • 好的...抱歉,这是我第一次使用 stackoverflow 解决问题...

标签: python image button tkinter layout


【解决方案1】:

这是因为您在button.config(image=photo2,...) 中将button2 拼错为button。应该是button2.config(image=photo2, ...)

【讨论】:

  • 非常感谢它的工作...我花了一个小时试图弄清楚...再次感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-11
  • 1970-01-01
  • 2015-02-20
  • 2021-05-26
  • 2021-03-10
  • 2013-03-24
相关资源
最近更新 更多