Python tkinter scrolledtext frame

Виджет Text

Text предназначен для отображения и редактирования многострочного текста. Стоит отметить, что данный виджет доступен только в основном пакете tkinter , в пакете tkinter.ttk аналога нет.

Основные параметры конструктора Text:

  • bd / borderwidth : толщина границы
  • bg/background : фоновый цвет
  • fg/foreground : цвет текста
  • font : шрифт текста, например, font=»Arial 14″ — шрифт Arial высотой 14px, или font=(«Verdana», 13, «bold») — шрифт Verdana высотой 13px с выделением жирным
  • height : высота в строках
  • padx : отступ от границ кнопки до ее текста справа и слева
  • pady : отступ от границ кнопки до ее текста сверху и снизу
  • relief : определяет тип границы, может принимать значения SUNKEN, RAISED, GROOVE, RIDGE
  • state : устанавливает состояние кнопки, может принимать значения DISABLED, ACTIVE, NORMAL (по умолчанию)
  • width : ширина в символах
  • wrap : указывает, каким образом переносить текст, если он не вмещается в границы виджета
from tkinter import * root = Tk() root.title("METANIT.COM") root.geometry("250x200") editor = Text() editor.pack(fill=BOTH, expand=1) root.mainloop()

виджет Test в tkinter и python

Перенос текста

Иногда предложения в текстовом поле могут быть очень большими, что могут не помещаться в отведенное для них пространство виджета. В этом случае большое значение имеет стратегия переноса, которая устанавливается с помощью параметра wrap . Этот параметр может принимать следующие параметры:

  • none : переносы отстуствуют, но можно сделать горизонтальную прокрутку
  • char : переносы осуществляются по символам
  • word : переносы осуществляются по словам
from tkinter import * root = Tk() root.title("METANIT.COM") root.geometry("250x200") char_editor = Text(height=5, wrap="char") char_editor.pack(anchor=N, fill=X) word_editor = Text(height=5, wrap="word") word_editor.pack(anchor=S, fill=X) root.mainloop()

Перенос текста в виджете Text в Tkinter и python

Прокрутка текста

Используя Scrollbar, можно добавить в Text прокрутку текста:

from tkinter import * from tkinter import ttk root = Tk() root.title("METANIT.COM") root.geometry("250x200") root.grid_columnconfigure(0, weight = 1) root.grid_rowconfigure(0, weight = 1) editor = Text(wrap = "none") editor.grid(column = 0, row = 0, sticky = NSEW) ys = ttk.Scrollbar(orient = "vertical", command = editor.yview) ys.grid(column = 1, row = 0, sticky = NS) xs = ttk.Scrollbar(orient = "horizontal", command = editor.xview) xs.grid(column = 0, row = 1, sticky = EW) editor["yscrollcommand"] = ys.set editor["xscrollcommand"] = xs.set root.mainloop()

Здесь для виджета определяются две полосы прокрутки — вертикальная и горизонтальная, соответственно, для каждой определяется свой элемент Scrollbar . Один (ys) имеет вертикальную ориентацию, а второй (xs) — горизонтальную. А у Text устанавливаются команды yscrollcommand и xscrollcommand с помощью соответствующих скроллбаров.

Читайте также:  React js css анимация

вертикальная и горизонтальная прокрутка в Text в Tkinter и Python

Стоит отметить, что поскольку создание прокрутки для виджета Text является довольно распространенной задачей, то в Tkinter также по умолчанию есть аналог виджета Text с готовой вертикальной прокруткой — ScrolledText (в пакете tkinter.scrolledtext):

from tkinter import * from tkinter.scrolledtext import ScrolledText root = Tk() root.title("METANIT.COM") root.geometry("250x150") st = ScrolledText(root, width=50, height=10) st.pack(fill=BOTH, side=LEFT, expand=True) root.mainloop()

Источник

Python tkinter scrolledtext frame

  • Python | Creating a button in tkinter
  • Python | Add style to tkinter button
  • Python | Add image on a Tkinter button
  • Python Tkinter – Label
  • Python Tkinter | Create LabelFrame and add widgets to it
  • RadioButton in Tkinter | Python
  • Python Tkinter – Checkbutton Widget
  • Python Tkinter – Canvas Widget
  • Python Tkinter | Create different shapes using Canvas class
  • Python Tkinter | Create different type of lines using Canvas class
  • Python Tkinter | Moving objects using Canvas.move() method
  • Combobox Widget in tkinter | Python
  • maxsize() method in Tkinter | Python
  • minsize() method in Tkinter | Python
  • resizable() method in Tkinter | Python
  • Python Tkinter – Entry Widget
  • Tkinter – Read only Entry Widget
  • Python Tkinter – Text Widget
  • Python Tkinter – Message
  • Python | Menu widget in Tkinter
  • Python Tkinter – Menubutton Widget
  • Python Tkinter – SpinBox
  • Progressbar widget in Tkinter | Python
  • Python-Tkinter Scrollbar
  • Python Tkinter – ScrolledText Widget
  • Python Tkinter – ListBox Widget
  • Scrollable ListBox in Python-tkinter
  • Python Tkinter – Frame Widget
  • Scrollable Frames in Tkinter
  • How to make a proper double scrollbar frame in Tkinter
  • Python Tkinter – Scale Widget
  • Hierarchical treeview in Python GUI application
  • Python-Tkinter Treeview scrollbar
  • Python Tkinter – Toplevel Widget
  • Python | askopenfile() function in Tkinter
  • Python | asksaveasfile() function in Tkinter
  • Python – Tkinter askquestion Dialog
  • Python Tkinter – MessageBox Widget
  • Create a Yes/No Message Box in Python using tkinter
  • Change the size of MessageBox – Tkinter
  • Different messages in Tkinter | Python
  • Change Icon for Tkinter MessageBox
  • Python – Tkinter Choose color Dialog
  • Popup Menu in Tkinter
  • Getting screen’s height and width using Tkinter | Python
  • Python | How to dynamically change text of Checkbutton
  • Python | focus_set() and focus_get() method
  • Search String in Text using Python-Tkinter
  • Autocomplete ComboBox in Python-Tkinter
  • Autohiding Scrollbars using Python-tkinter
  • Python Tkinter – Validating Entry Widget
  • Tracing Tkinter variables in Python
  • Python | setting and retrieving values of Tkinter variable
  • Tkinter | Adding style to the input text using ttk.Entry widget
  • Python | after method in Tkinter
  • destroy() method in Tkinter | Python
  • Text detection using Python
  • Python | winfo_ismapped() and winfo_exists() in Tkinter
  • Collapsible Pane in Tkinter | Python
  • Creating a multiple Selection using Tkinter
  • Creating Tabbed Widget With Python-Tkinter
  • Open a new Window with a button in Python-Tkinter
  • Cryptography GUI using python
  • Python | Simple GUI calculator using Tkinter
  • Create Table Using Tkinter
  • Python | GUI Calendar using Tkinter
  • File Explorer in Python using Tkinter
  • Python | ToDo GUI Application using Tkinter
  • Python: Weight Conversion GUI using Tkinter
  • Python: Age Calculator using Tkinter
  • Python | Create a GUI Marksheet using Tkinter
  • Python | Loan calculator using Tkinter
  • Python | Create a digital clock using Tkinter
  • Make Notepad using Tkinter
  • Color game using Tkinter in Python
  • Python | Simple FLAMES game using Tkinter
  • Simple registration form using Python Tkinter
  • How to create a COVID19 Data Representation GUI?
  • Python | Creating a button in tkinter
  • Python | Add style to tkinter button
  • Python | Add image on a Tkinter button
  • Python Tkinter – Label
  • Python Tkinter | Create LabelFrame and add widgets to it
  • RadioButton in Tkinter | Python
  • Python Tkinter – Checkbutton Widget
  • Python Tkinter – Canvas Widget
  • Python Tkinter | Create different shapes using Canvas class
  • Python Tkinter | Create different type of lines using Canvas class
  • Python Tkinter | Moving objects using Canvas.move() method
  • Combobox Widget in tkinter | Python
  • maxsize() method in Tkinter | Python
  • minsize() method in Tkinter | Python
  • resizable() method in Tkinter | Python
  • Python Tkinter – Entry Widget
  • Tkinter – Read only Entry Widget
  • Python Tkinter – Text Widget
  • Python Tkinter – Message
  • Python | Menu widget in Tkinter
  • Python Tkinter – Menubutton Widget
  • Python Tkinter – SpinBox
  • Progressbar widget in Tkinter | Python
  • Python-Tkinter Scrollbar
  • Python Tkinter – ScrolledText Widget
  • Python Tkinter – ListBox Widget
  • Scrollable ListBox in Python-tkinter
  • Python Tkinter – Frame Widget
  • Scrollable Frames in Tkinter
  • How to make a proper double scrollbar frame in Tkinter
  • Python Tkinter – Scale Widget
  • Hierarchical treeview in Python GUI application
  • Python-Tkinter Treeview scrollbar
  • Python Tkinter – Toplevel Widget
  • Python | askopenfile() function in Tkinter
  • Python | asksaveasfile() function in Tkinter
  • Python – Tkinter askquestion Dialog
  • Python Tkinter – MessageBox Widget
  • Create a Yes/No Message Box in Python using tkinter
  • Change the size of MessageBox – Tkinter
  • Different messages in Tkinter | Python
  • Change Icon for Tkinter MessageBox
  • Python – Tkinter Choose color Dialog
  • Popup Menu in Tkinter
  • Getting screen’s height and width using Tkinter | Python
  • Python | How to dynamically change text of Checkbutton
  • Python | focus_set() and focus_get() method
  • Search String in Text using Python-Tkinter
  • Autocomplete ComboBox in Python-Tkinter
  • Autohiding Scrollbars using Python-tkinter
  • Python Tkinter – Validating Entry Widget
  • Tracing Tkinter variables in Python
  • Python | setting and retrieving values of Tkinter variable
  • Tkinter | Adding style to the input text using ttk.Entry widget
  • Python | after method in Tkinter
  • destroy() method in Tkinter | Python
  • Text detection using Python
  • Python | winfo_ismapped() and winfo_exists() in Tkinter
  • Collapsible Pane in Tkinter | Python
  • Creating a multiple Selection using Tkinter
  • Creating Tabbed Widget With Python-Tkinter
  • Open a new Window with a button in Python-Tkinter
  • Cryptography GUI using python
  • Python | Simple GUI calculator using Tkinter
  • Create Table Using Tkinter
  • Python | GUI Calendar using Tkinter
  • File Explorer in Python using Tkinter
  • Python | ToDo GUI Application using Tkinter
  • Python: Weight Conversion GUI using Tkinter
  • Python: Age Calculator using Tkinter
  • Python | Create a GUI Marksheet using Tkinter
  • Python | Loan calculator using Tkinter
  • Python | Create a digital clock using Tkinter
  • Make Notepad using Tkinter
  • Color game using Tkinter in Python
  • Python | Simple FLAMES game using Tkinter
  • Simple registration form using Python Tkinter
  • How to create a COVID19 Data Representation GUI?

Источник

Tkinter ScrolledText

Summary: in this tutorial, you’ll learn how to use the Tkinter ScrolledText widget that consists of a Text widget and vertical Scrollbar widget.

Introduction to the Tkinter ScrolledText widget

So far, you’ve learned how to create a Text widget and how to link a vertical Scrollbar to the text widget.

To make it more convenient, Tkinter provides you with the ScrolledText widget which does the same things as a text widget linked to a vertical scroll bar.

To use the ScrolledText widget, you need to import the ScrolledText class from the tkinter.scrolledtext module.

Technically, the ScrolledText class inherits from the Text class.

The ScrolledText widget uses a Frame widget inserted between the container and the Text widget to hold the Scrollbar widget.

Therefore, the ScrolledText has the same properties and methods as the Text widget. In addition, the geometry manager methods including pack, grid, and place are restricted to the Frame .

Tkinter ScrolledText widget example

The following program illustrates how to create a ScrolledText widget:

import tkinter as tk from tkinter.scrolledtext import ScrolledText root = tk.Tk() root.title("ScrolledText Widget") st = ScrolledText(root, width=50, height=10) st.pack(fill=tk.BOTH, side=tk.LEFT, expand=True) root.mainloop()Code language: JavaScript (javascript)
  • First, import the tkinter module and the ScrolledText class from the tkinter.scrolledtext module.
  • Second, create the root window and set its title to ‘ScrolledText Widget’ .
  • Third, create a new ScrolledText widget and display it on the root window.
  • Finally, start the main loop.

Here’s the same program but written using the object-oriented programming approach:

import tkinter as tk from tkinter.scrolledtext import ScrolledText class App(tk.Tk): def __init__(self): super().__init__() self.title("ScrolledText Widget") st = ScrolledText(self, width=50, height=10) st.pack(fill=tk.BOTH, side=tk.LEFT, expand=True) if __name__ == "__main__": app = App() app.mainloop() Code language: Python (python)

Summary

Источник

Оцените статью