简体   繁体   English

Tkinter:并排证明两个框架

[英]Tkinter : Justifying two frames side by side

I am writing a small game as a part of my UNI project , and so far I have written this code :我正在编写一个小游戏作为我的 UNI 项目的一部分,到目前为止我已经编写了以下代码:

import tkinter as tk
from tkinter import *
from tkinter import messagebox
import random

root = Tk()
linear_array = [i for i in range(1,26)]
random_array = []
removed_numbers=[]
number_to_send = None
num_to_recv = None

#Creating Frames
frame1 = Frame(root)
frame1.pack(side=TOP,fill=X)

frame2 = Frame(root)
frame2.pack(side=TOP,fill=X,padx=10,pady=10)

frame3 = Frame(root)
frame3.pack(side=TOP,fill=X,pady=5)

#Button click function
def numberClick(num,btn):
    global number_to_send
    #messagebox.showinfo('Message',str(num)+' is removed')
    number_to_send = num
    removed_numbers.append(num)
    btn.configure(text='X')
    btn.configure(bg='red',fg='white')
    btn.configure(state="disabled")
    print(removed_numbers)
    stmnt = str(num)+" was removed!!"
    textBox.delete('1.0', END)
    textBox.insert(END,stmnt)
    textBox.tag_add("center", 1.0, "end")

#Generating linear array and a random array from the linear array
for i in range(1,26):
    temp = random.choice(linear_array)
    linear_array.remove(temp)
    random_array.append(temp) 

#Generating Title bar
title = Label(frame1,text='B . I . N . G . O !!!',fg='red',bg='yellow')
title.config(font=("Courier", 30))
title.pack(side=TOP,fill=X)

#Generating a 5x5 Button matrix
rows=5
columns=5
btns = [[None for i in range(rows)] for j in range(columns)]
button_mapping = {}
for i in range(rows):
    for j in range(columns):
        num = random.choice(random_array)
        random_array.remove(num)
        btns[i][j]=Button(frame2, text = num , fg ='red',height = 3, width = 5)
        btns[i][j]['command']=lambda btn=btns[i][j],num=num: numberClick(num,btn)
        btns[i][j]['borderwidth'] = 2
        btns[i][j]['relief'] = "groove"
        btns[i][j].grid(row=i,column=j)
        button_mapping[num]=btns[i][j]

#Printing numbers which are deleted 
textBox = Text(frame3, height = 1, width = 28)
textBox.tag_configure("center", justify='center')
textBox.insert("1.0","Recently Deleted Number")
textBox.tag_add("center", 1.0, "end")
textBox.pack(side=TOP)
   
root.mainloop()

The output is as shown :输出如图所示:

在此处输入图片说明

Now I want to add two more frames as shown below :现在我想再添加两个框架,如下所示: 在此处输入图片说明

How can I do the same?我怎样才能做同样的事情? I tried frame in frame and also aligning frame2 to LEFT and frame4 to RIGHT but it didn't work out.我尝试了逐帧,并将 frame2 与 LEFT 对齐,frame4 与 RIGHT 对齐,但没有成功。 Please help.请帮忙。 TIA!蒂亚!

You can create a frame, eg frame , to hold the two frames ( frame4 and frame5 ), then set side=LEFT for frame2 and side=RIGHT for frame .可以创建一个帧,例如frame ,以保持两帧( frame4frame5 ),则组side=LEFT对于frame2side=RIGHTframe But you need to change the order of packing the frames as below:但是您需要更改打包框架的顺序,如下所示:

#Creating Frames
frame1 = Frame(root)
frame1.pack(side=TOP, fill=X)

frame3 = Frame(root)
frame3.pack(side=BOTTOM, fill=X, pady=5)

frame2 = Frame(root)
frame2.pack(side=LEFT, fill=Y, padx=10, pady=10)

frame = Frame(root) # parent of frame4 and frame5
frame.pack(side=RIGHT, fill=BOTH, expand=True, padx=10, pady=10)
# make frame4 and frame5 use all the space
frame.grid_columnconfigure(0, weight=1)
frame.grid_rowconfigure(0, weight=1)
frame.grid_rowconfigure(1, weight=1)

Then you can create frame4 and frame5 inside frame as below:然后您可以在frame内创建frame4frame5 ,如下所示:

frame4 = Frame(frame, bd=1, relief='solid')
frame4.grid(sticky='nsew', padx=5, pady=5)

frame4 = Frame(frame, bd=1, relief='solid')
frame4.grid(sticky='nsew', padx=5, pady=5)

And the output:和输出:

在此处输入图片说明

You could try using a grid.您可以尝试使用网格。 If that doesn't work just comment and I'll delete the answer, I just don't have a high enough rep to comment.如果这不起作用,只需发表评论,我将删除答案,我只是没有足够高的代表来发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM