如何在特定的地方放置按钮?

时间:2015-11-19 17:03:18

标签: python python-3.x tkinter

我希望使用坐标之类的东西来放置按钮。我尝试过使用pack和grid。如果使用网格或确定每列/行的大小,我希望能够跳过列。每次数字增加一个额外的数字时,它会按下按钮并使其看起来很奇怪。反正有没有避免这个?如果有办法继续使用网格或包并修复我的问题,我该怎么做?

#Date: November 25, 2015
#FIle: TestingStatsGUI.py

import tkinter as tk

class PickStats(tk.Frame):
    health_attr = 0
    health = 90
    energy = 45
    energy_attr = 0
    def __init__(self, master):
        tk.Frame.__init__(self, master, background="Black")
        tk.activeforeground= "blak"
        master = master
        self.init_window()

    def init_window(self):
        self.master.title("Player")
        root.geometry("850x550")
        self.pack(fill="both", expand=1)


        self.update_window()

    def update_window(self):
        self.healthAdd = tk.Button(self, text="+", font= 13, fg='white', bg='#000080',
                                   width = 2,
                                   command=lambda: self.update("addHealth"))
        self.healthSub = tk.Button(self, text="--", font=13, fg='white', bg='red',
                                   width = 2,
                                   command=lambda: self.update("subHealth"))
        self.HealthTotal = tk.Label(self, text="Health: {}".format(self.health),
                                   font=15, fg='#01ffff', bg='black')
        self.healthAdd.config(height = 0)
        self.HealthTotal.grid(row=1, column=0, columnspan=3)
        self.healthAdd.grid(row=1, column=10, padx=6)
        self.healthSub.grid(row=1, column=20)


    def update(self, method):
        if method == "addHealth":
            self.health_attr += 1
        elif method == "subHealth":
            if self.health_attr <= 0:
                pass
            else:
                self.health_attr -= 1
        self.health = 90 + self.health_attr*4
        self.update_window()
        print(self.health)


root = tk.Tk()
app = PickStats(root)
root.mainloop()

1 个答案:

答案 0 :(得分:0)

要解决按钮被推过的特定问题,您只需要给标签留出足够的空间来增长。例如,您需要做的就是更改一行:

self.HealthTotal = tk.Label(..., width=16)

您的代码中存在严重错误 - 每按一下按钮就会创建越来越多的小部件。您只需要创建一次小部件。从那时起,您可以使用单个语句更改显示的值。

例如:

def init_window(self):
    self.master.title("Player")
    root.geometry("850x550")
    self.pack(fill="both", expand=1)

    self.healthAdd = tk.Button(self, text="+", font= 13, fg='white', bg='#000080',
                               width = 2,
                               command=lambda: self.update("addHealth"))
    self.healthSub = tk.Button(self, text="--", font=13, fg='white', bg='red',
                               width = 2,
                               command=lambda: self.update("subHealth"))
    self.HealthTotal = tk.Label(self, text="Health: {}".format(self.health),
                                font=15, fg='#01ffff', bg='black', width=16)
    self.healthAdd.config(height = 0)
    self.HealthTotal.grid(row=1, column=0, columnspan=3)
    self.healthAdd.grid(row=1, column=10, padx=6)
    self.healthSub.grid(row=1, column=20)

def update_window(self):
    self.HealthTotal.configure(text="Health: {}".format(self.health))

顺便说一句,不包含任何内容的行和列的宽度或高度为零。如果第1-19列中没有任何内容,则将内容放入第10列或第20列是没有意义的。