0

I have a menu button in a GUI I am making that displays a popup containing a CSV file, using pd.read_csv through pandas. However, there is a lot of data, and when the popup is displayed, pandas cuts off a lot of the data, only displaying the data in the beginning and in the end of the file.

I want to be able to scroll through all the data in my popup window. Any suggestions?

Here is the code for the popup command:

def popuptable():
    popup = tk.Tk()
    popup.wm_title("!")
    label = ttk.Label(popup, text=(pd.read_csv('NameofCSVFile.csv')), font=NORM_FONT)
    label.pack(side="top", fill="x", pady=10)
    popup.mainloop()
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Andrew Louis
  • 303
  • 1
  • 2
  • 15
  • 1
    BTW: `Tkinter` should use only one `Tk()` - to create second window use `Toplevel()`. And it needs only one `mainloop()`. If you use second `mainloop()` it can behave strange. – furas Jan 12 '17 at 15:46
  • to scroll element you will have to use `Canvas()` with this element and scroll canvas. Or use [ScrolledText()](https://docs.python.org/3.5/library/tkinter.scrolledtext.html#module-tkinter.scrolledtext) which can be scrolled. – furas Jan 12 '17 at 15:49
  • @furas can i use Canvas() within my popup? – Andrew Louis Jan 12 '17 at 15:51
  • `Canvas` is normal widget (like `Label` or `Button`) so you can use it in `Tk`/`Toplevel` but creating scrolled `Canvas` sometimes makes problem - you can find example on SO. – furas Jan 12 '17 at 15:53
  • [scrollbar with Text or Canvas](http://effbot.org/zone/tkinter-scrollbar-patterns.htm) – furas Jan 12 '17 at 15:56
  • http://stackoverflow.com/questions/3085696/adding-a-scrollbar-to-a-group-of-widgets-in-tkinter – furas Jan 12 '17 at 15:57

1 Answers1

1

I give you an example of how to do a scrollable text widget that looks like a label. I put it in the main window for this example, but you just have to replace root by a toplevel to adapt it to your case.

from tkinter import Tk, Text, Scrollbar

root = Tk()
# only the column containing the text is resized when the window size changes:
root.columnconfigure(0, weight=1) 
# resize row 0 height when the window is resized
root.rowconfigure(0, weight=1)

txt = Text(root)
txt.grid(row=0, column=0, sticky="eswn")

scroll_y = Scrollbar(root, orient="vertical", command=txt.yview)
scroll_y.grid(row=0, column=1, sticky="ns")
# bind txt to scrollbar
txt.configure(yscrollcommand=scroll_y.set)

very_long_list = "\n".join([str(i) for i in range(100)])

txt.insert("1.0", very_long_list)
# make the text look like a label
txt.configure(state="disabled", relief="flat", bg=root.cget("bg"))

root.mainloop()

Screenshot:

screenshot

j_4321
  • 15,431
  • 3
  • 34
  • 61