简体   繁体   中英

Python tkinter: how to make the widgets in the middle of window?

I'm new to TK. How to make the scrlledText and buttons in the middle of the window.

I executed the code and got it like the picture below with the Green line, but I want the style to be like the red line.

Any idea? Thanks.

window= Tk()
window.geometry('500x500')
window.resizable(0,0)
window.title("Server")

msgText = scrolledtext.ScrolledText(window, width=50, height=10, font=("宋体", 10),wrap=WORD)
msgText.grid(row=0, column=0,padx = 20,pady = 10)
self.statusText = scrolledtext.ScrolledText(window, width=50, height=10, font=("宋体", 10),wrap=WORD)
statusText.grid(row=1, column=0,padx = 20,pady = 10)

openFileButton = Button(window, width = 15, text="选择文件", command=self.openFile)
openFileButton.grid(row=2, column=0, sticky=W)


sendButton = Button(window, text='发送文件', width = 15,command=self.sendFile)
sendButton.grid(row=2, column=1, sticky=E)

在此处输入图像描述

It appears all that you need to do is add columnspan=2 when adding the scrolledtext widgets to the display. However, it's impossible for me to know for certain since you didn't provide a proper [mcve].

msgText.grid(row=0, column=0,padx = 20,pady = 10, columnspan=2)
statusText.grid(row=1, column=0,padx = 20,pady = 10, columnspan=2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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