使用tkinter时,我有一个类,当我引用文本输入框时,它完全弄乱了布局/大小
之前:This shows what it should be
代码:
from tkinter import ttk
from tkinter import *
class ModifiedMixin:
'''
Class to allow a Tkinter Text widget to notice when it's modified.
To use this mixin, subclass from Tkinter.Text and the mixin, then write
an __init__() method for the new class that calls _init().
Then override the beenModified() method to implement the behavior that
you want to happen when the Text is modified.
'''
def _init(self):
'''
Prepare the Text for modification notification.
'''
# Clear the modified flag, as a side effect this also gives the
# instance a _resetting_modified_flag attribute.
self.clearModifiedFlag()
# Bind the <<Modified>> virtual event to the internal callback.
self.bind_all('<<Modified>>', self._beenModified)
def _beenModified(self, event=None):
'''
Call the user callback. Clear the Tk 'modified' variable of the Text.
'''
# If this is being called recursively as a result of the call to
# clearModifiedFlag() immediately below, then we do nothing.
if self._resetting_modified_flag: return
# Clear the Tk 'modified' variable.
self.clearModifiedFlag()
# Call the user-defined callback.
self.beenModified(event)
def beenModified(self, event=None):
'''
Override this method in your class to do what you want when the Text
is modified.
'''
pass
def clearModifiedFlag(self):
'''
Clear the Tk 'modified' variable of the Text.
Uses the _resetting_modified_flag attribute as a sentinel against
triggering _beenModified() recursively when setting 'modified' to 0.
'''
# Set the sentinel.
self._resetting_modified_flag = True
try:
# Set 'modified' to 0. This will also trigger the <<Modified>>
# virtual event which is why we need the sentinel.
self.tk.call(self._w, 'edit', 'modified', 0)
finally:
# Clean the sentinel.
self._resetting_modified_flag = False
if __name__ == '__main__':
from tkinter import Text, BOTH
class TextInput(ModifiedMixin, Text):
'''
Subclass both ModifiedMixin and Tkinter.Text.
'''
def __init__(self, *a, **b):
# Create self as a Text.
Text.__init__(self, *a, **b)
# Initialize the ModifiedMixin.
self._init()
def beenModified(self, event=None):
'''
Override this method do do work when the Text is modified.
'''
print(self.get("1.0","end-1c"))
root = Tk()
# This is the section of code which creates the main window
root.geometry('189x62')
root.configure(background='#F0F8FF')
root.title('Test')
# This is the section of code which creates a text input box
tInput=Entry(root)
tInput.place(x=25, y=20)
## Opening and saving ##
with open("YES.txt") as f:
all_names = f.readlines()
all_names = [x.strip() for x in all_names]
## Searching ##
def searching():
search_term = getInputBoxValue()
print (search_term)
found_names = filter(lambda name: search_term in name, all_names)
results = list(found_names)
print (results)
if search_term == "break":
pass
else:
pass
root.mainloop()
开课后:This shows what happens when I add it, also same thing when I drag it to make it bigger
代码后:
from tkinter import ttk
from tkinter import *
class ModifiedMixin:
'''
Class to allow a Tkinter Text widget to notice when it's modified.
To use this mixin, subclass from Tkinter.Text and the mixin, then write
an __init__() method for the new class that calls _init().
Then override the beenModified() method to implement the behavior that
you want to happen when the Text is modified.
'''
def _init(self):
'''
Prepare the Text for modification notification.
'''
# Clear the modified flag, as a side effect this also gives the
# instance a _resetting_modified_flag attribute.
self.clearModifiedFlag()
# Bind the <<Modified>> virtual event to the internal callback.
self.bind_all('<<Modified>>', self._beenModified)
def _beenModified(self, event=None):
'''
Call the user callback. Clear the Tk 'modified' variable of the Text.
'''
# If this is being called recursively as a result of the call to
# clearModifiedFlag() immediately below, then we do nothing.
if self._resetting_modified_flag: return
# Clear the Tk 'modified' variable.
self.clearModifiedFlag()
# Call the user-defined callback.
self.beenModified(event)
def beenModified(self, event=None):
'''
Override this method in your class to do what you want when the Text
is modified.
'''
pass
def clearModifiedFlag(self):
'''
Clear the Tk 'modified' variable of the Text.
Uses the _resetting_modified_flag attribute as a sentinel against
triggering _beenModified() recursively when setting 'modified' to 0.
'''
# Set the sentinel.
self._resetting_modified_flag = True
try:
# Set 'modified' to 0. This will also trigger the <<Modified>>
# virtual event which is why we need the sentinel.
self.tk.call(self._w, 'edit', 'modified', 0)
finally:
# Clean the sentinel.
self._resetting_modified_flag = False
if __name__ == '__main__':
from tkinter import Text, BOTH
class TextInput(ModifiedMixin, Text):
'''
Subclass both ModifiedMixin and Tkinter.Text.
'''
def __init__(self, *a, **b):
# Create self as a Text.
Text.__init__(self, *a, **b)
# Initialize the ModifiedMixin.
self._init()
def beenModified(self, event=None):
'''
Override this method do do work when the Text is modified.
'''
print(self.get("1.0","end-1c"))
root = Tk()
# This is the section of code which creates the main window
root.geometry('189x62')
root.configure(background='#F0F8FF')
root.title('Test')
# This is the section of code which creates a text input box
tInput=TextInput()
tInput.place(x=25, y=20)
## Opening and saving ##
with open("YES.txt") as f:
all_names = f.readlines()
all_names = [x.strip() for x in all_names]
## Searching ##
def searching():
search_term = getInputBoxValue()
print (search_term)
found_names = filter(lambda name: search_term in name, all_names)
results = list(found_names)
print (results)
if search_term == "break":
pass
else:
pass
root.mainloop()
因此,您看到顶部图片中的文本输入框在中间。当我给它上课时,尽管它移至最底端并保持原状。我希望它在上课时保持相同的位置
(很抱歉,如果格式不正确,这是我在这里的第一个问题。)
(编辑,抱歉,让我发布完整的代码。)
答案 0 :(得分:0)
没关系,我已修复它。我要做的是将这两行放入类函数中:
self.configure(宽度= 20,高度= 1) self.place(x = 19,y = 247)
干杯!