如何精确移动Tkinter小部件。放置方法不起作用

2024-09-27 02:25:42 发布

您现在位置:Python中文网/ 问答频道 /正文

以下是目前为止的代码:

# -*- coding: utf-8 -*-

from Tkinter import *

#Creates game window
master = Tk()
master.geometry("640x480")
master.resizable(width = False, height = False)
master.title("YeeHaw Poker")

#Divides window into subsections
menuFrame = Frame(master, bg = "black", height = 60)
menuFrame.pack(fill = X, side = TOP)
tableFrame = Frame(master, highlightbackground = "black", highlightthickness = 4)
tableFrame.pack(fill = BOTH, expand = True)
optionsFrame = Frame(master, bg = "black", height = 100)
optionsFrame.pack(fill = X, side = BOTTOM)

#Draws poker table decorations
tableDecorations = Canvas(tableFrame, bg = "#771427", highlightthickness = 0)
tableDecorations.pack(fill = BOTH, expand = True)
#Renders window thus far so that dimensions can be found
master.update()
tWidth = tableDecorations.winfo_width()
tHeight = tableDecorations.winfo_height()
#Main edge
gap = 10
tableDecorations.create_rectangle(gap, gap, tWidth - gap, tHeight - gap, fill ="#277714", width = 4)
#Table outline
gap = 30
tableDecorations.create_rectangle(gap, gap, tWidth - gap, tHeight - gap, outline = "#35a31b", width = 2)
#Card outline coordinates
cardNum = 5
cardSize = 20
cardHeight = cardSize * 3.5
cardWidth = cardSize * 2.5
cardSpace = 10
cardTop = tHeight / 4
cardLeft = (tWidth - (cardNum * (cardWidth + cardSpace))) / 2
cardY1 = cardTop + cardHeight
cardY2 = cardTop
cardX1 = [0 for i in range(0, cardNum)]
cardX2 = [0 for i in range(0, cardNum)]
suit = [0 for i in range(0, cardNum)]

for i in range(0, cardNum):
    cardX1[i] = cardLeft + (i * (cardWidth + cardSpace))
    cardX2[i] = cardX1[i] + cardWidth
    suit[i] = Label(tableDecorations, text = "", bg = "white", font = (None, 50))
    suit[i].place(x = 5000, y  = 5000)

#Draws specified card in specified place
def drawCard(pos, type, pip):
    if type == "empty":
        tableDecorations.create_rectangle(cardX1[pos], cardY1, cardX2[pos], cardY2, outline = "#35a31b", width = 2)
        suit[pos].pack_forget()
    else:
        tableDecorations.create_rectangle(cardX1[pos], cardY1, cardX2[pos], cardY2, fill = "white", outline = "grey", width = 1)
        if type == "diamond":
            suit[pos].config(text = "♦", fg = "red")
        elif type == "heart":
            suit[pos].config(text = "♥", fg = "red")
        elif type == "spade":
            suit[pos].config(text = "♠", fg = "black")
        elif type == "club":
            suit[pos].config(text = "♣", fg = "black")
        suit[pos].pack()

#Creates new table
def newTable():
    for i in range(0, cardNum):
        drawCard(i, "diamond", 0)

newTable()

master.mainloop()

但是,这根本不会移动带有钻石的标签,如下所示:

Diamonds not moving

真让人恼火。。。在

我希望钻石出现在每张卡片上,但很明显这不是在这里发生的。。。在

有什么想法吗?在


Tags: inposmasterfortypewidthfillpack

热门问题