把桌子放在中间

2024-10-01 00:14:04 发布

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

我正在用tkinter:)试着把“StackQuestion”表放在中间。你知道吗

我不知道什么是选择中心水平,或垂直的表内的接收容器。你知道吗

第一个类StackQuestion定义了一般windows如何工作以及如何从一个容器切换到另一个容器。第二,定义表及其内容。你知道吗

我试着给不同的容器添加不同的颜色,以检查哪个容器是活动的,从而尝试将容器内的所有内容集中起来,但没有成功。你知道吗

编辑:为了不在这里有大量的代码,我删除了所有其他类和重复“测试”在“为F在(测试,测试):”为了代码在您的计算机上运行。你知道吗

table to center

#utf8
#python3
import tkinter as tk
from tkinter import *
from tkinter import ttk


class StackQuestion(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        container = ttk.Frame(self)

        container.grid()

        #windows resize management
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (Test, Test):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=1, column=1, sticky='nsew')

        self.show_frame(Test)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()


class Test(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)


        label = tk.Label(self, text = 'StackQuestion')
        label.grid(row=0, column=0, columnspan=2)

        questions = ['Question 1', 'Question 2' , 'Question 3']
        row_number = 1
        column_number = 0

        Tree = ttk.Treeview(self, show ='headings')
        Tree['columns'] = ('Author', 'Subject', 'Date', 'Question')
        Tree.heading('Author', text = 'Author')
        Tree.heading('Subject', text = 'Subject')
        Tree.heading('Date', text = 'Date')
        Tree.heading('Question', text = 'Question')



        for value in questions :
            Tree.insert('', 'end', value, text = value)
            Tree.set(value, 'Author', value)


        Tree.grid()


app = StackQuestion()
app.geometry('1080x700')
app.configure(background='grey')
app.mainloop()

Tags: texttestselftreeinitvaluetkintercontainer
1条回答
网友
1楼 · 发布于 2024-10-01 00:14:04

我不是一个专业的python程序员,事实上我还是一个新手,为了学习我也很努力。所以我根据toti08的提示找到了这个“noob fix”

你可以改变

 container.grid()

使用:

container.pack(pady=50)

这将使它水平居中,并给桌子一点填充顶部,这样你也可以垂直居中,或至少给它一点边缘。你知道吗

希望这能有所帮助,直到有更有经验的人提出解决您问题的方案:)

相关问题 更多 >