我在Tkinter中遇到了网格管理和图像更改方面的困难。

huangapple go评论125阅读模式
英文:

i am having difficulty in grid management and image changes in tkinter

问题

我想要创建一个剪刀石头布游戏并使用tkinter和Python创建其GUI但输出混乱不堪
顺便说一句我不是计算机科学背景我是生物学背景只是刚开始学习Python作为一种爱好
图像重叠在一起列大小不合适如何修复这些问题

from tkinter import *
import random
from PIL import ImageTk, Image
main = Tk()

###主窗口位置###
main_width = 700
main_height = 570
screen_width = main.winfo_screenwidth()
screen_height = main.winfo_screenheight()
xpoint = (screen_width/2) - (main_width/2)
ypoint = (screen_height/2) - (main_height/2)
main.geometry("%dx%d+%d+%d" % (main_width, main_height, xpoint, ypoint))
main.title("剪刀石头布游戏")
back = ImageTk.PhotoImage(Image.open("mainbg2.jpg"))
backimg = Label(main, image=back).grid(row=2, columnspan=4)

##问题在于backimg没有被两张新的玩家和电脑手的图片替换,而是被它们重叠了。###

###有用的变量###
rockimg = ImageTk.PhotoImage(Image.open("rock.gif"))
scissorimg = ImageTk.PhotoImage(Image.open("scissor.gif"))
paperimg = ImageTk.PhotoImage(Image.open("paper.gif"))

###按钮操作###
def rock(y):
    rocklabel = Label(main, image=rockimg)
    rocklabel.grid(row=2, column=0, columnspan=2, sticky=W)
    rocklabel.image = rockimg
    game(y)

def paper(y):

    paperlabel = Label(main, image=paperimg)
    paperlabel.grid(row=2, column=0, columnspan=2, sticky=W)
    paperlabel.image = paperimg
    game(y)

def scissor(y):

    scissorlabel = Label(main, image=scissorimg)
    scissorlabel.grid(row=2, column=0, columnspan=2, sticky=W)
    scissorlabel.image = scissorimg
    game(y)

###主要逻辑###

x = [rockimg, paperimg, scissorimg]

def game(y):
    ##电脑手的显示##
    intial = random.choice(x)
    ##计算结果##
    value = y - x.index(intial)
    ###问题在于玩家手和电脑手的图片之间的距离太远了###
    output = Label(main, image=intial).grid(row=2, column=2, columnspan=2)
    if value == 2 or value == -1:
        lost = Label(main, text="你输了").grid(row=4, column=0)
    elif value == 1 or value == -2:
        win = Label(main, text="你赢了").grid(row=4, column=0)
    else:
        tie = Label(main, text="平局").grid(row=4, column=0)

###按钮和标签###

###问题在于行和列的排列不够美观,按钮之间的间距混乱###
mainheading = Label(main, text='剪刀石头布游戏', padx=4, pady=7, width=20)
mainheading.grid(row=0, column=0, columnspan=4)
rock_button = Button(main, text="石头", command=lambda: rock(0), height=2, width=23)
rock_button.grid(row=4, column=0, sticky=W+E, padx=25)
paper_button = Button(main, text="纸", command=lambda: paper(1), height=2, width=23)
paper_button.grid(row=4, column=1, sticky=W+E)
scissor_button = Button(main, text="剪刀", command=lambda: scissor(2), height=2, width=23)
scissor_button.grid(row=4, column=2, sticky=W)
exit_button = Button(main, text="退出", command=main.destroy).grid(row=5, column=3)
main.mainloop()
英文:

I want to build a rock paper scissor game. and its GUI using tkinter and python.but the output is messed up.
ps. I am not from cs background I am from biology background, just started to learn python as a hobby.
the images overlap each other, and the column sizes are not proper. how to fix these.

from tkinter import *
import random
from PIL import ImageTk , Image
main = Tk()
###main window position###
main_width = 700
main_height = 570
screen_width = main.winfo_screenwidth()
screen_height = main.winfo_screenheight()
xpoint = (screen_width/2) - (main_width/2)
ypoint = (screen_height/2) - (main_height/2)
main.geometry("%dx%d+%d+%d" %(main_width, main_height, xpoint, ypoint))
main.title("Rock,Paper,Scissor game")
back = ImageTk.PhotoImage(Image.open("mainbg2.jpg"))
backimg = Label(main, image = back).grid(row = 2, columnspan =4)
##the problem here is the backimg is not replace by the 2 new images of player and computer hand, it just is overlapped by them.###
###usefull variables###
rockimg = ImageTk.PhotoImage(Image.open("rock.gif"))
scissorimg = ImageTk.PhotoImage(Image.open("scissor.gif"))
paperimg = ImageTk.PhotoImage(Image.open("paper.gif"))
###button actions###
def rock(y):
rocklabel = Label(main,image= rockimg )
rocklabel.grid(row=2, column=0, columnspan = 2, sticky = W)
rocklabel.image=rockimg
game(y)
def paper(y):
paperlabel = Label(main, image = paperimg)
paperlabel.grid(row=2, column=0,columnspan = 2,sticky = W)
paperlabel.image = paperimg
game(y)
def scissor(y):
scissorlabel= Label(main, image = scissorimg)
scissorlabel.grid(row=2, column=0, columnspan = 2,sticky = W)
scissorlabel.image = scissorimg
game(y)
###main logic###
x=[ rockimg, paperimg, scissorimg]
def game(y):
##computer hand print##
intial= random.choice(x)
##formula##
value= y - x.index(intial)
###the problem here is the player hand image and computer hand image are too wide apart###
output= Label(main, image = intial).grid(row=2, column=2, columnspan=2)
if value==2 or value == -1:
lost = Label(main, text = " You lost").grid(row = 4, column= 0)
elif value == 1 or value == -2:
win = Label(main, text = " You win").grid(row = 4, column= 0)
else:
tie = Label(main, text = " It's a tie").grid(row = 4, column= 0)
###buttons and labels###
###the problem here is the arrangement of the rows and columns is not pretty, the spacing between the buttons is messedup###
mainheading = Label(main, text = 'Rock Paper Scissor game', padx = 4, pady = 7, width=20)
mainheading.grid(row = 0, column = 0, columnspan = 4)
rock_button = Button(main, text = "ROCK",  command = lambda:rock(0) , height = 2 ,width = 23)
rock_button.grid(row = 4, column = 0 ,  sticky = W+E, padx = 25)
paper_button = Button(main, text = "PAPER",  command = lambda:paper(1), height = 2 ,width = 23)
paper_button.grid(row = 4, column = 1,sticky = W+E , )
scissor_button = Button(main, text = "SCISSOR",  command = lambda:scissor(2), height = 2 ,width = 23)
scissor_button.grid(row = 4, column = 2,sticky = W ,)
exit_button = Button(main, text = "exit" , command = main.destroy).grid(row = 5 , column = 3)
main.mainloop()

答案1

得分: 0

如果你只想要删除旧标签,那么grid_forget()可能是你唯一需要做的。我添加了第五列,这样对我来说看起来更好,如果你的图像大小相同,由于文本长度差异而导致的调整将消失。你也可以像处理文本一样使用config方法,但我个人更喜欢创建图像实例,然后只使用grid_forget方法。

示例代码(我用文本替换了你的图像,因为我没有它们):

###我将标签创建移到函数外,用你的图像替换###
rocklabel = Label(main, text='ROCK')
#rocklabel = Label(main,image= rockimg )
#rocklabel.image=rockimg
paperlabel = Label(main, text='PAPER')
scissorlabel= Label(main, text='SCISSOR')

###为对手图像也创建一个集合,用你的图像替换文本###
oppRock = Label(main, text='ROCK')
oppPaper = Label(main, text='PAPER')
oppScissor= Label(main, text='SCISSOR')

###初始游戏窗口的占位符图像###
inilabel = Label(main, text="Let's play!")
inilabel.grid(row=2, column=1)
output= Label(main, text='Opponent')
output.grid(row=2, column=3)
result = Label(main, text='Result')
result.grid(row=5, column=0, columnspan=5, pady=10)

###按钮操作,现在你可以在创建的标签上调用grid_forget方法###
def rock(y):
    paperlabel.grid_forget()
    scissorlabel.grid_forget()
    rocklabel.grid(row=2, column=1)
    game(y)

def paper(y):
    rocklabel.grid_forget()
    scissorlabel.grid_forget()
    paperlabel.grid(row=2, column=1)
    game(y)

def scissor(y):
    rocklabel.grid_forget()
    paperlabel.grid_forget()
    scissorlabel.grid(row=2, column=1)
    game(y)

###主要逻辑###
x = ['ROCK', 'PAPER', 'SCISSOR']

def game(y):
##计算机手势显示##
    inilabel.grid_forget()
    intial = random.choice(x)
##计算公式##
    value = y - x.index(intial)
    output.config(text=intial)
###考虑到随机选择,添加适当的对手图像###
    if intial == 'ROCK':
        oppPaper.grid_forget()
        oppScissor.grid_forget()
        oppRock.grid(row=2, column=3)
    elif intial == 'PAPER':
        oppRock.grid_forget()
        oppScissor.grid_forget()
        oppPaper.grid(row=2, column=3)
    else:
        oppRock.grid_forget()
        oppPaper.grid_forget()
        oppScissor.grid(row=2, column=3)

###在文本更改时使用config选项,而不是创建新对象###
    if value == 2 or value == -1:
        result.config(text=" You lost")
    elif value == 1 or value == -2:
        result.config(text=" You win")
    else:
        result.config(text="It's a tie")

###按钮和标签###

mainheading = Label(main, text='Rock Paper Scissor game', padx=4, pady=7, width=20)
mainheading.grid(row=0, column=0, columnspan=5)
rock_button = Button(main, text="ROCK", command=lambda: rock(0), height=2, width=23)
rock_button.grid(row=4, column=0)
paper_button = Button(main, text="PAPER", command=lambda: paper(1), height=2, width=23)
paper_button.grid(row=4, column=2)
scissor_button = Button(main, text="SCISSOR", command=lambda: scissor(2), height=2, width=23)
scissor_button.grid(row=4, column=4)
exit_button = Button(main, text="exit", command=main.destroy).grid(row=6, columnspan=5, pady=10)
英文:

if all you want to do is remove the old labels then grid_forget() is probably all you need to do. I added a fifth column so that it looked a bit better to me, if your images are the same size, the resizing due to the text length difference will disappear. You could also also use the config method just like with text but I personally prefer the idea of creating the image instance then just using grid_forget method.

sample code (i replaced your images with text since i don't have them):

###I brought the label creation out of the function, replace with your images###
rocklabel = Label(main, text = 'ROCK')
#rocklabel = Label(main,image= rockimg )
#rocklabel.image=rockimg
paperlabel = Label(main, text = 'PAPER')
scissorlabel= Label(main, text ='SCISSOR')
###a set for opponent images also, replace text with your images###
oppRock = Label(main, text = 'ROCK')
oppPaper = Label(main, text = 'PAPER')
oppScissor= Label(main, text ='SCISSOR')
###placeholder images for initial game window###
inilabel = Label(main, text = "Let's play!")
inilabel.grid(row=2, column=1)
output= Label(main, text = 'Opponent')
output.grid(row=2, column=3)
result = Label(main, text = 'Result')
result.grid(row = 5, column= 0, columnspan=5, pady = 10)
###button actions, now you can call grid forget method on the created labels###
def rock(y):
paperlabel.grid_forget()
scissorlabel.grid_forget()
rocklabel.grid(row=2, column=1,)
game(y)
def paper(y):
rocklabel.grid_forget()
scissorlabel.grid_forget()
paperlabel.grid(row=2, column=1)
game(y)
def scissor(y):
rocklabel.grid_forget()
paperlabel.grid_forget()
scissorlabel.grid(row=2, column=1)
game(y)
###main logic###
x=[ 'ROCK', 'PAPER', 'SCISSOR']
def game(y):
##computer hand print##
inilabel.grid_forget()
intial= random.choice(x)
##formula##
value= y - x.index(intial)
output.config(text = intial)
###accounting for the random choice, add appropriate opp image###
if intial == 'ROCK':
oppPaper.grid_forget()
oppScissor.grid_forget()
oppRock.grid(row=2, column=3)
elif intial == 'PAPER':
oppRock.grid_forget()
oppScissor.grid_forget()
oppPaper.grid(row=2, column=3)
else:
oppRock.grid_forget()
oppPaper.grid_forget()
oppScissor.grid(row=2, column=3)
###use config options on a text change rather than new object###
if value==2 or value == -1:
result.config(text = " You lost")
elif value == 1 or value == -2:
result.config(text = " You win")
else:
result.config(text = "It's a tie")
###buttons and labels###
mainheading = Label(main, text = 'Rock Paper Scissor game', padx = 4, pady = 7, 
width=20)
mainheading.grid(row = 0, column = 0, columnspan = 5)
rock_button = Button(main, text = "ROCK",  command = lambda:rock(0) , height = 2 
,width = 23)
rock_button.grid(row = 4, column = 0)
paper_button = Button(main, text = "PAPER",  command = lambda:paper(1), height = 2 
,width = 23)
paper_button.grid(row = 4, column = 2)
scissor_button = Button(main, text = "SCISSOR",  command = lambda:scissor(2), height 
= 2 ,width = 23)
scissor_button.grid(row = 4, column = 4)
exit_button = Button(main, text = "exit" , command = main.destroy).grid(row = 6 , 
columnspan = 5, pady=10)

huangapple
  • 本文由 发表于 2020年1月3日 20:46:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578908.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定