Найти - Пользователи
Полная версия: Tkinter. SrcollBar не скроллирует текст в Entry - только при применении ООП.
Начало » GUI » Tkinter. SrcollBar не скроллирует текст в Entry - только при применении ООП.
1
streetmover
Доброго времени суток.

Python версии 2.7.6

Текст в Entry скроллировать вправо-влево не получается, хотя, пока похожий код не был “упакован” в класс - все работало (см. второй фрагмент). Как починить?

код:

# -*- coding: utf-8 -*-
from Tkinter import *
class MainWindow():
    def __init__(self, master=None):
        self.MainWindow = Tk()
        self.createWidgets()
        self.MainWindow.mainloop()
    def scrEnt(event, *L):
        
        if event == 'scroll':
                direction = L[0]
                discontinuity = L[1]
                self.ent.xview_scroll(direction, discontinuity)
        elif event == 'moveto':
                direction = L[0]
                self.ent.xview_moveto(direction)
    def createWidgets(self):
        self.scr = Scrollbar(self.MainWindow, orient = HORIZONTAL, command = self.scrEnt)
        self.ent = Entry(self.MainWindow, font='Arial 12', xscrollcommand = self.scr.set)
        self.ent.pack()
        self.scr.pack(fill = BOTH)
mw = MainWindow()

———————————————————–
а вот так, без ООП - работает:

# -*- coding: utf-8 -*-
from Tkinter import *
def scrEnt(event, *L):
    
    if event == 'scroll':
            direction = L[0]
            discontinuity = L[1]
            ent.xview_scroll(direction, discontinuity)
    elif event == 'moveto':
            direction = L[0]
            ent.xview_moveto(direction)
MainWindow = Tk()
scr = Scrollbar(MainWindow, orient = HORIZONTAL, command = scrEnt)
ent = Entry(MainWindow, font='Arial 12', xscrollcommand = scr.set)
ent.pack()
scr.pack(fill = BOTH)
MainWindow.mainloop()
terabayt
а как же self?!
def scrEnt(self, event, *L):
# . . .
class MainWindow():
    def __init__(self, master=None):
        self.MainWindow = Tk()
        self.createWidgets()
        self.MainWindow.mainloop()
    def scrEnt(self, event, *L):
        if event == 'scroll':
                direction = L[0]
                discontinuity = L[1]
                self.ent.xview_scroll(direction, discontinuity)
        elif event == 'moveto':
                direction = L[0]
                self.ent.xview_moveto(direction)
# . . .
P.S.
from Tkinter import *
так не делайте!
streetmover
terabayt

Спасибо.
4kpt_III
class MainWindow():
    def __init__(self, master=None):
        self.MainWindow = Tk()
        self.createWidgets()
        self.MainWindow.mainloop()

Лихо. И нафиг здесь ООП…
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB