0

I really need to change the size of textbox but I keep on getting nothing. Here is code:

import tkinter as tk 
from tkinter import * 
from tkinter import ttk


class App(object):
    def __init__(self, master, **kwarg):
        self.master = master #self.master is calling 1 argument from function. = master is giving some sort of name u can call later from this function and class
        self.open_chat()
    def open_chat(self):    
        self.textbox = tk.Text(self.master, wrap = WORD) #wrap = WPRD moves the text to a new line as soon as it is reaching the end of pannel
        #pannel for entering the text   adding self to make it global for all the functions in the class
        self.chatbox = tk.Text(self.master, wrap = "word")#showing the text
        scroll = ttk.Scrollbar(self.master)
        send = ttk.Button(self.master, text = "Send", command = self.send_a_message)
        scroll.config(command=self.chatbox.yview)
        self.chatbox.config(yscrollcommand=scroll.set, state = "disabled")
        self.chatbox.grid(column=0, row=0,sticky='NSWE')
        self.textbox.grid(column=0, row=1,sticky='NSWE')
        send.grid(column=1,row=1,sticky='NSW')
        scroll.grid(column=1, row=0, sticky='NSW')

    def send_a_message(self):
        self.chatbox.config(state="normal")
        content = self.textbox.get('1.0','end') #from 0 first char to the end
        self.chatbox.insert(END, content) #insert(END, text) to append text to the widget.
        self.chatbox.config(state="disabled")
        self.textbox.delete('1.0',END)

win = tk.Tk()
win.title ('My program') #program name on the left top corner
win.minsize(1000,500)
app = App(win)
win.mainloop()

I tried changing height and width, but in the end i got my widget to disappear

3
  • What have you tried on changing the width and height of textbox?
    – acw1668
    Commented Sep 6 at 6:20
  • I tried changing size with grid, options as height width when i was creating actual widget, tried changing <grid_rowconfigure> and for now nothing helped. My widget keep on being huge
    – Misaki
    Commented Sep 6 at 6:28
  • width and height cannot be configured using .grid(). It is better to post a minimal reproducible example.
    – acw1668
    Commented Sep 6 at 6:31

1 Answer 1

1

It's not entirely clear how you want the text widgets to resize, but if you want them to change dynamically (which I assume based on the sticky='NSWE' portion) when using grid, you'll need to use grid_rowconfigure and grid_columnconfigure. These methods are called on the container widget and take the number of the row/column in question, along with a relative weight.

In your case you could add this after the self.master = master line and before self.open_chat() in the __init__:

self.master.grid_rowconfigure(0, weight=1)
self.master.grid_rowconfigure(1, weight=1)
self.master.grid_columnconfigure(0, weight=1)
2
  • 1
    Btw you can do .grid_rowconfigure((0, 1), weight=1). Not really that useful in this example but great if you have to grid_*configure a lot of rows/columns
    – TheLizzard
    Commented Sep 6 at 0:16
  • Well. I tried the way u told me and go a new mistake:) AttributeError: '_tkinter.tkapp' object has no attribute 'grid_rowconfig'
    – Misaki
    Commented Sep 6 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.