0

The below codes are for creating a table with Text() function of tkinter. I want to add a scrollbar to this window but I guess when I add scrollbar to the Text(), this method doesn't serve for scrolling the window, it's for scrolling inside the Text().

I didn't add a geometry to this example. The current geometry depends on the row and column amount. But suppose to be a fixed geometry size is requested and when we write the row and column amount, we should see the other rows and columns with this scrollbar.

from tkinter import *


class Application:
    def __init__(self):

        self.window = Tk()
        self.window.title("Database")
        self.window.resizable(width=False, height=False)

        self.frame_1 = Frame(bg="black")
        self.frame_1.grid(row=0, column=0)

        self.menubar = Menu(tearoff=0)

        self.table()
        self.menu()
        self.mainloop = self.window.mainloop()

    def table(self):
        row = int(input("Row:"))
        column = int(input("Column:"))
        for i in range(row):
            for j in range(column):
                text = Text(self.frame_1, width=20, height=1)
                text.bind("<Button-3>", self.popup)
                text.grid(row=i, column=j, padx=1, pady=1)
                text.configure(state="disabled")

    def menu(self):
        self.menubar.add_command(label="Copy", command=lambda: self.window.focus_get().event_generate('<<Copy>>'))

    def popup(self, event):
        self.menubar.post(event.x_root, event.y_root)
James Z
  • 12,209
  • 10
  • 24
  • 44
dildeolupbiten
  • 1,314
  • 1
  • 15
  • 27

1 Answers1

0

You cannot scroll widgets added to a text widget with grid. The text widget can only scroll widgets embedded with window_create.

The most common technique is to place all of the widgets in a frame, add that frame to a canvas, and then scroll the canvas. See Adding a scrollbar to a group of widgets in Tkinter for an example.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • I modified the codes according to post that you wrote. I put a scrollbar both to x and y axises. The codes that you wrote are working. But i want to ask a different question which is about the amount of the row and columns. For example according to the tests, max row*col is 18*18, when i type 19*19, the program is waiting as if it's calculating. Also it accepts 100*3 but not accepts 100*4. Do you know the reason? – dildeolupbiten Jun 15 '17 at 18:51
  • @dildeolupbiten: if you want to ask another question, click the "Ask Question" button. Stackoverflow isn't designed to support long conversations in the comments. – Bryan Oakley Jun 15 '17 at 18:54
  • Oh ok. I will do. – dildeolupbiten Jun 15 '17 at 18:54