I was using C# GTK (Gtk#), but when I asked ChatGPT how to add a scrollbar to MessageDialog
, it answered in Python, and it seems Python GTK is more popular than C# GTK, so I tried to test in in Python. ChatGPT's code added a scrollbar, but the line number was limited to 2, when the screen is large.
I want the MessageDialog to grow to show more lines up to a reasonable height. Of course, I do not want a fixed tall size; if there are only a few lines, than there is no need to take a lot of height. Is that possible?
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
lines = []
for i in range(0, 100):
line = "Line {}".format(i)
lines.append(line)
joined = "\n".join(lines)
# Create the message dialog
dialog = Gtk.MessageDialog(parent=None, flags=0, message_type=Gtk.MessageType.INFO,
buttons=Gtk.ButtonsType.OK_CANCEL, text="original")
# Create a scrolled window and add the message to it
scrolled_window = Gtk.ScrolledWindow()
scrolled_window.set_border_width(10)
scrolled_window.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
tv = Gtk.TextView()
tv.get_buffer().set_text(joined)
scrolled_window.add_with_viewport(tv)
# Add the scrolled window to the dialog
dialog.vbox.pack_start(scrolled_window, True, True, 0)
dialog.show_all()
# Run the dialog
response = dialog.run()
dialog.destroy()