python tkinter的grid()网格不在两个frame容器里互通

下面是代码

import tkinter as tk

root = tk.Tk()
pixel=tk.PhotoImage(width=1,height=1)

mwbns=["基础abcd训练","英文练习","中文练习"]

face1=tk.Frame(root)

def MianWindow():
    face1.grid()

    l1=tk.Label(face1,text="Hello,user!",font=("normal",50))
    l1.grid(pady=200,column=1,columnspan=1200)

    l2=tk.Label(face1,text="你现在还没有登录呢",font=("normal",10))
    l2.grid(column=1,columnspan=400)

def Window1():
    face1.destroy()
    face2=tk.Frame(root)
    face2.grid()
    b1=tk.Button(face2,text="",image=pixel,width=100,height=40,command=None,compound="c")
    b1.grid(column=1,columnspan=100)

def Window2():
    face1.destroy()
    face2=tk.Frame(root)
    face2.grid()

def Window3():
    face1.destroy()
    face2=tk.Frame(root)
    face2.grid()

mwbcs=[Window1,Window2,Window3]

for i in range(3):
    ab=tk.Button(root,text=mwbns[i],image=pixel,width=400,height=40,command=mwbcs[i],compound="c")
    ab.grid(row=0,column=i)

def main():
    root.geometry("1200x700")
    root.title("typer")
    root.resizable(0,0)

    MianWindow()

    tk.mainloop()

if __name__=="__main__":
    main()

注意这个MainWindow函数与Window1函数,还有这个放在外面的for循环,明明column是1运行出来却是:

python tkinter的grid()的参数(column,row...)不在两个frame容器里互通
还有:

python tkinter的grid()的参数(column,row...)不在两个frame容器里互通
这是为什么呢又要怎样才能让他们的网格是一样的?

讨论数量: 1
Jason990420

明明 column 是 1

在容器中, column 0 不存在, 所以不会显示; column 1 看起来就会像是 column 0.

怎样才能让他们的网格是一样的?

最好是使用同一容器, 比如

from tkinter import *

root = Tk()

frame = Frame(root)
frame.grid()

items = ["基础abcd训练","英文练习","中文练习"]

buttons, labels = [], []

for column, item in enumerate(items):

    button = Button(frame, text=item)
    button.grid(row=0, column=column)
    buttons.append(button)

    label = Label(frame, text=item)
    label.grid(row=1, column=column)
    labels.append(label)

root.mainloop()

file

11个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!