Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在TKinter中沿网格边添加按钮_Python_Button_Python 3.x_Tkinter_Grid - Fatal编程技术网

Python 如何在TKinter中沿网格边添加按钮

Python 如何在TKinter中沿网格边添加按钮,python,button,python-3.x,tkinter,grid,Python,Button,Python 3.x,Tkinter,Grid,我正在为TKinter中的游戏创建一个网格,我希望在蓝色瓷砖网格的下方或旁边有按钮。我已经试过了: from tkinter import * class BattleScreen(Frame): def __init__(self, root): Frame.__init__(self, root) self.grid() for row in range(20): for col in range(20):

我正在为TKinter中的游戏创建一个网格,我希望在蓝色瓷砖网格的下方或旁边有按钮。我已经试过了:

from tkinter import *

class BattleScreen(Frame):
    def __init__(self, root):
        Frame.__init__(self, root)
        self.grid()

        for row in range(20):
           for col in range(20):
                butt1 = Button(self, bg='blue', width=1)
                butt1.grid(row=row, column=col)

        #self.but_frame = Frame(self)
        #self.but_frame.pack(fill=X)
        button1 = Button(self, text='Quit', width=6, command=lambda root=root:root.destroy())
        button1.grid(row=21)


root = Tk()
sheet = BattleScreen(root)
root.mainloop()
运行此操作时,会在蓝色瓷砖下方创建按钮,但由于所有按钮都位于同一网格上,且按钮更宽,因此会将按钮上方的所有内容都弄乱

您看到的注释掉的两行代码是我试图创建另一个独立于网格框架的框架来放置按钮,但我想您不能这样做。我错了吗

如何在网格中获得蓝色瓷砖下方或旁边的按钮,而不破坏蓝色瓷砖的对齐?

用于使按钮单元格跨多列:

button1.grid(row=20, columnspan=20)

用于使按钮单元格跨越多列:

button1.grid(row=20, columnspan=20)

用于使按钮单元格跨越多列:

button1.grid(row=20, columnspan=20)

用于使按钮单元格跨越多列:

button1.grid(row=20, columnspan=20)

对我来说,最简单的方法是使用两个框架——一个用于按钮网格,另一个用于其他按钮。然后你可以把这些东西从上到下并排打包。然后,将网格用于按钮网格,将网格或包用于其他按钮

通过这种方式,网格和按钮在逻辑上是不同的,您可以对它们进行布局,而不必考虑它们对其余显示的影响。这使您的程序更易于维护并随着时间的推移而增长

请注意,在下面的代码中,我将
self.grid()
BattleScreen
中删除——我认为框架将自身置于其父框架中是不好的做法。父级应控制放置

from tkinter import *

class BattleScreen(Frame):
    def __init__(self, root):
        Frame.__init__(self, root)

        for row in range(20):
           for col in range(20):
                butt1 = Button(self, bg='blue', width=1)
                butt1.grid(row=row, column=col)


class Controls(Frame):
    def __init__(self, root):
        Frame.__init__(self, root)
        self.quit = Button(self, text="Quit", width=6, 
                           command=root.destroy)
        self.quit.pack()

root = Tk()
screen = BattleScreen(root)
controls = Controls(root)
controls.pack(side="bottom", fill="x")
screen.pack(side="top", fill="both", expand=True)
root.mainloop()

对我来说,最简单的方法是使用两个框架——一个用于按钮网格,另一个用于其他按钮。然后你可以把这些东西从上到下并排打包。然后,将网格用于按钮网格,将网格或包用于其他按钮

通过这种方式,网格和按钮在逻辑上是不同的,您可以对它们进行布局,而不必考虑它们对其余显示的影响。这使您的程序更易于维护并随着时间的推移而增长

请注意,在下面的代码中,我将
self.grid()
BattleScreen
中删除——我认为框架将自身置于其父框架中是不好的做法。父级应控制放置

from tkinter import *

class BattleScreen(Frame):
    def __init__(self, root):
        Frame.__init__(self, root)

        for row in range(20):
           for col in range(20):
                butt1 = Button(self, bg='blue', width=1)
                butt1.grid(row=row, column=col)


class Controls(Frame):
    def __init__(self, root):
        Frame.__init__(self, root)
        self.quit = Button(self, text="Quit", width=6, 
                           command=root.destroy)
        self.quit.pack()

root = Tk()
screen = BattleScreen(root)
controls = Controls(root)
controls.pack(side="bottom", fill="x")
screen.pack(side="top", fill="both", expand=True)
root.mainloop()

对我来说,最简单的方法是使用两个框架——一个用于按钮网格,另一个用于其他按钮。然后你可以把这些东西从上到下并排打包。然后,将网格用于按钮网格,将网格或包用于其他按钮

通过这种方式,网格和按钮在逻辑上是不同的,您可以对它们进行布局,而不必考虑它们对其余显示的影响。这使您的程序更易于维护并随着时间的推移而增长

请注意,在下面的代码中,我将
self.grid()
BattleScreen
中删除——我认为框架将自身置于其父框架中是不好的做法。父级应控制放置

from tkinter import *

class BattleScreen(Frame):
    def __init__(self, root):
        Frame.__init__(self, root)

        for row in range(20):
           for col in range(20):
                butt1 = Button(self, bg='blue', width=1)
                butt1.grid(row=row, column=col)


class Controls(Frame):
    def __init__(self, root):
        Frame.__init__(self, root)
        self.quit = Button(self, text="Quit", width=6, 
                           command=root.destroy)
        self.quit.pack()

root = Tk()
screen = BattleScreen(root)
controls = Controls(root)
controls.pack(side="bottom", fill="x")
screen.pack(side="top", fill="both", expand=True)
root.mainloop()

对我来说,最简单的方法是使用两个框架——一个用于按钮网格,另一个用于其他按钮。然后你可以把这些东西从上到下并排打包。然后,将网格用于按钮网格,将网格或包用于其他按钮

通过这种方式,网格和按钮在逻辑上是不同的,您可以对它们进行布局,而不必考虑它们对其余显示的影响。这使您的程序更易于维护并随着时间的推移而增长

请注意,在下面的代码中,我将
self.grid()
BattleScreen
中删除——我认为框架将自身置于其父框架中是不好的做法。父级应控制放置

from tkinter import *

class BattleScreen(Frame):
    def __init__(self, root):
        Frame.__init__(self, root)

        for row in range(20):
           for col in range(20):
                butt1 = Button(self, bg='blue', width=1)
                butt1.grid(row=row, column=col)


class Controls(Frame):
    def __init__(self, root):
        Frame.__init__(self, root)
        self.quit = Button(self, text="Quit", width=6, 
                           command=root.destroy)
        self.quit.pack()

root = Tk()
screen = BattleScreen(root)
controls = Controls(root)
controls.pack(side="bottom", fill="x")
screen.pack(side="top", fill="both", expand=True)
root.mainloop()

它是战列舰吗?它是战列舰吗?它是战列舰吗?它是战列舰吗