0

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)

enter image description here

Ringo
  • 1,173
  • 1
  • 12
  • 25
  • 1
    Related: https://stackoverflow.com/questions/18736465/how-to-center-a-tkinter-widget/18738568 – Thaer A Apr 26 '20 at 05:54
  • 1
    @ThaerA thanks, but it can't meet my request. because I want the widgets in the middle between right and left instead of the center of the window. Pls see the picture in the question above. – Ringo Apr 26 '20 at 06:02
  • It seems that it can modify the `relx` and `rely` to adjust. Thanks again. – Ringo Apr 26 '20 at 06:05
  • You could use a frame,and put all of your widgets in the frame,and use `.grid()`.That's wouldn't make a big change in your code. – jizhihaoSAMA Apr 26 '20 at 06:28
  • Read [tkinter gui layout using frames and grid](https://stackoverflow.com/a/34277295/7414759) – stovfl Apr 26 '20 at 08:25
  • Please create a [mcve]. We can't run the code as posted. – Bryan Oakley Apr 26 '20 at 15:53

1 Answers1

0

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)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685