Tkinter 在使用嵌套 类 时在输入框中显示输出
Tkinter displaying output in a entry box using when using nested classes
所以问题是在使用第二个 window 时(您使用下一页按钮导航到它)。当我在每个框中输入 8 位二进制数时,前提是它应该将它们加在一起并显示在标记为添加的二进制框中。但目前它似乎没有正确设置值来做到这一点,它只是在命令行中打印我设置的值,这表明该方法正在运行。
如果能深入了解如何解决此问题,我们将不胜感激。
仅供参考,它是故意为 8 位二进制完成的,是的,它需要使用嵌套 类。因为这就是作业需要使用的东西。
对不起,如果这只是一个小错误,我在某处很笨拙,但我已经被难住了一段时间。
这是修改后的最小可重现示例:
from tkinter import *
from tkinter.ttk import *
from tkinter import ttk
import tkinter
import math
import sys
def BinaryAddition(value1, value2):
if len(value1) != 8:
return "Binary number 1 is not 8 bits"
if len(value2) != 8:
return "Binary number 2 is not 8 bits"
res = bin(int(value1,2) + int(value2,2))
Ob = res[2:]#removing the ob prefix
return Ob
class MainWindow():
FONT = ("Consolas", 16)
TxtMaxLen = 32
class SecondWindow():
def __init__(self2):
self2._window = tkinter.Tk()
self2._window.title("Converter")
self2._window["bg"] = "#EFFFA3"
f = ("Times bold", 16)
TxtMaxLen = 32
self2._window.geometry("820x240")
label = tkinter.Label(self2._window, text="Binary Number 1: ", font= f)#label defined for number input box
label.grid(row=0, column=0, padx=10, pady=5)#positioning / dimensions of input box
label = tkinter.Label(self2._window, text="Binary Number 2: ", font= f)#label defined for number input box
label.grid(row=1, column=0, padx=10, pady=5)#positioning / dimensions of input box
#input box for binary number 1
self2.input1 = tkinter.Entry(self2._window, width= TxtMaxLen, font= f) #defined input box
self2.input1.grid(row=0, column=1, pady=5)#postioning / dimensions of input box
self2.input1.focus()
#input box for binary number 2
self2.input2 = tkinter.Entry(self2._window, width=TxtMaxLen, font= f) #defined input box
self2.input2.grid(row=1, column=1, pady=5)#postioning / dimensions of input box
self2.input2.focus()
separator = tkinter.ttk.Separator(self2._window,orient=tkinter.HORIZONTAL)
separator.grid(row=3, column=1, pady=15)
self2._bt_Add = tkinter.Button(self2._window, text="Add", font= f, command = self2.AdditionSelection)#button defined for bin
self2._bt_Add.grid(row=1, column=2, padx=5, pady=5)#postioning / dimensions of button box
#labels for output box of combined binary number
label = tkinter.Label(self2._window, text="Added Binary: ", font= f)#label defined for number input box
label.grid(row=4, column=0, padx=10, pady=5)#positioning / dimensions of input box
#label for output box for if there was or was not an overflow
label = tkinter.Label(self2._window, text="OverFlow: ", font= f)#label defined for number input box
label.grid(row=5, column=0, padx=10, pady=5)#positioning / dimensions of input box
#output box for the added binary number
self2.stringvar_Combined = tkinter.StringVar()
txt_output = tkinter.Entry(self2._window, textvariable=self2.stringvar_Combined, width=TxtMaxLen, state="readonly", font= f)#entry box set to readonly to act as a display box
txt_output.grid(row=4, column=1, pady=5)
#output box for if there was or was not an overflow
self2._stringvar_OverFlow = tkinter.StringVar()
txt_output = tkinter.Entry(self2._window, textvariable=self2._stringvar_OverFlow, width=TxtMaxLen, state="readonly", font= f)#entry box set to readonly to act as a display box
txt_output.grid(row=5, column=1, pady=5)
separator = tkinter.ttk.Separator(self2._window,orient=tkinter.VERTICAL)
separator.grid(row=3, column=2, pady=15)
PrevPageButton = tkinter.Button(self2._window, text="Previous Page", font=f, command=MainWindow)
PrevPageButton.grid(row=5, column = 2, padx = 5,pady = 5)
def set_values(self, BinAdd):
self.stringvar_Combined.set(BinAdd)
def AdditionSelection(self2):
try:
BinV1 = self2.input1.get().strip().replace(" ", "")
BinV2 = self2.input2.get().strip().replace(" ", "")
BinAdd = BinaryAddition(BinV1, BinV2)
self2.set_values(BinAdd)
print(BinAdd)
print("hi there")
except Exception as ex:
print("NOPE it aint workin")
def __init__(self):
self._window = tkinter.Tk()
self._window.title("Converter")
self._window["bg"] = "#20A3FF" #background colour
self._window.geometry("820x240")#setting window size
#setting up button for binary additon page
NextPageButton = tkinter.Button(self._window, text="Next Page", font=MainWindow.FONT, command=self.SecondWindow)
NextPageButton.grid(row=4, column = 3, padx = 5,pady = 5)
self._window.destroy
def mainloop(self):
self._window.mainloop()
if __name__ == "__main__":
win = MainWindow()
win.mainloop()
主要问题:要创建第二个 window,您应该使用 Toplevel()
而不是 Tk()
。
当我使用 Toplevel
时,它会在 window 中显示文本。
如果你只想看到一个 window 那么你应该先 destroy()
旧的 window 然后你可以使用 Tk()
创建新的 window(新 mainloop()
)。
但这会产生其他问题。它会删除 window 中的所有值,当您返回一些 window 时,它将不再具有旧值。创建两个 Frames
并在不破坏它们的情况下将它们替换为 Tk()
可能更好 - 它们将保留旧值。
其余只是关于如何创建更具可读性的代码的建议:
- 不要把
SecondWindow
放在MainWindow
里面
- 不要使用
self2
- 对函数和变量使用
lower_case_names
PEP 8 -- Style Guide for Python Code
中的更多内容
代码有一些变化。
显示数值(我还加了.zfill(8)
显示8位)
但是每次当您更改 window - next
或 previous
时,这都会创建新的 window - 因此您可能有两个主要的 windows,等等。它需要更多的改变。
import tkinter # PEP8: `import *` is not preferred
from tkinter import ttk
import math
import sys
def binary_addition(value1, value2):
if len(value1) != 8:
return "Binary number 1 is not 8 bits"
if len(value2) != 8:
return "Binary number 2 is not 8 bits"
res = bin(int(value1,2) + int(value2,2))
return res[2:].zfill(8) #removing the ob prefix
class MainWindow():
FONT = ("Consolas", 16)
TxtMaxLen = 32
def __init__(self):
self._window = tkinter.Tk()
self._window.title("Converter")
self._window["bg"] = "#20A3FF" #background colour
self._window.geometry("820x240")#setting window size
#setting up button for binary additon page
next_page_button = tkinter.Button(self._window, text="Next Page", font=MainWindow.FONT, command=SecondWindow)
next_page_button.grid(row=4, column = 3, padx = 5,pady = 5)
def mainloop(self):
self._window.mainloop()
class SecondWindow():
def __init__(self):
self._window = tkinter.Toplevel()
self._window.title("Converter")
self._window["bg"] = "#EFFFA3"
f = ("Times bold", 16)
TxtMaxLen = 32
self._window.geometry("820x240")
label = tkinter.Label(self._window, text="Binary Number 1: ", font= f)#label defined for number input box
label.grid(row=0, column=0, padx=10, pady=5)#positioning / dimensions of input box
label = tkinter.Label(self._window, text="Binary Number 2: ", font= f)#label defined for number input box
label.grid(row=1, column=0, padx=10, pady=5)#positioning / dimensions of input box
#input box for binary number 1
self.input1 = tkinter.Entry(self._window, width= TxtMaxLen, font= f) #defined input box
self.input1.grid(row=0, column=1, pady=5)#postioning / dimensions of input box
self.input1.focus()
#input box for binary number 2
self.input2 = tkinter.Entry(self._window, width=TxtMaxLen, font= f) #defined input box
self.input2.grid(row=1, column=1, pady=5)#postioning / dimensions of input box
self.input2.focus()
separator = tkinter.ttk.Separator(self._window,orient=tkinter.HORIZONTAL)
separator.grid(row=3, column=1, pady=15)
self._bt_add = tkinter.Button(self._window, text="Add", font= f, command = self.addition_selection)#button defined for bin
self._bt_add.grid(row=1, column=2, padx=5, pady=5)#postioning / dimensions of button box
#labels for output box of combined binary number
label = tkinter.Label(self._window, text="Added Binary: ", font= f)#label defined for number input box
label.grid(row=4, column=0, padx=10, pady=5)#positioning / dimensions of input box
#label for output box for if there was or was not an overflow
label = tkinter.Label(self._window, text="OverFlow: ", font= f)#label defined for number input box
label.grid(row=5, column=0, padx=10, pady=5)#positioning / dimensions of input box
#output box for the added binary number
self.stringvar_combined = tkinter.StringVar()
txt_output = tkinter.Entry(self._window, textvariable=self.stringvar_combined, width=TxtMaxLen, state="readonly", font= f)#entry box set to readonly to act as a display box
txt_output.grid(row=4, column=1, pady=5)
#output box for if there was or was not an overflow
self._stringvar_overflow = tkinter.StringVar()
txt_output = tkinter.Entry(self._window, textvariable=self._stringvar_overflow, width=TxtMaxLen, state="readonly", font= f)#entry box set to readonly to act as a display box
txt_output.grid(row=5, column=1, pady=5)
separator = tkinter.ttk.Separator(self._window,orient=tkinter.VERTICAL)
separator.grid(row=3, column=2, pady=15)
prev_page_button = tkinter.Button(self._window, text="Previous Page", font=f, command=MainWindow)
prev_page_button.grid(row=5, column = 2, padx = 5,pady = 5)
def set_values(self, value):
self.stringvar_combined.set(value)
def addition_selection(self):
try:
bin_1 = self.input1.get().strip().replace(" ", "")
bin_2 = self.input2.get().strip().replace(" ", "")
bin_add = binary_addition(bin_1, bin_2)
self.set_values(bin_add)
print("bin_add:", bin_add)
except Exception as ex:
print("NOPE it aint workin", ex)
if __name__ == "__main__":
win = MainWindow()
win.mainloop()
所以问题是在使用第二个 window 时(您使用下一页按钮导航到它)。当我在每个框中输入 8 位二进制数时,前提是它应该将它们加在一起并显示在标记为添加的二进制框中。但目前它似乎没有正确设置值来做到这一点,它只是在命令行中打印我设置的值,这表明该方法正在运行。
如果能深入了解如何解决此问题,我们将不胜感激。 仅供参考,它是故意为 8 位二进制完成的,是的,它需要使用嵌套 类。因为这就是作业需要使用的东西。
对不起,如果这只是一个小错误,我在某处很笨拙,但我已经被难住了一段时间。
这是修改后的最小可重现示例:
from tkinter import *
from tkinter.ttk import *
from tkinter import ttk
import tkinter
import math
import sys
def BinaryAddition(value1, value2):
if len(value1) != 8:
return "Binary number 1 is not 8 bits"
if len(value2) != 8:
return "Binary number 2 is not 8 bits"
res = bin(int(value1,2) + int(value2,2))
Ob = res[2:]#removing the ob prefix
return Ob
class MainWindow():
FONT = ("Consolas", 16)
TxtMaxLen = 32
class SecondWindow():
def __init__(self2):
self2._window = tkinter.Tk()
self2._window.title("Converter")
self2._window["bg"] = "#EFFFA3"
f = ("Times bold", 16)
TxtMaxLen = 32
self2._window.geometry("820x240")
label = tkinter.Label(self2._window, text="Binary Number 1: ", font= f)#label defined for number input box
label.grid(row=0, column=0, padx=10, pady=5)#positioning / dimensions of input box
label = tkinter.Label(self2._window, text="Binary Number 2: ", font= f)#label defined for number input box
label.grid(row=1, column=0, padx=10, pady=5)#positioning / dimensions of input box
#input box for binary number 1
self2.input1 = tkinter.Entry(self2._window, width= TxtMaxLen, font= f) #defined input box
self2.input1.grid(row=0, column=1, pady=5)#postioning / dimensions of input box
self2.input1.focus()
#input box for binary number 2
self2.input2 = tkinter.Entry(self2._window, width=TxtMaxLen, font= f) #defined input box
self2.input2.grid(row=1, column=1, pady=5)#postioning / dimensions of input box
self2.input2.focus()
separator = tkinter.ttk.Separator(self2._window,orient=tkinter.HORIZONTAL)
separator.grid(row=3, column=1, pady=15)
self2._bt_Add = tkinter.Button(self2._window, text="Add", font= f, command = self2.AdditionSelection)#button defined for bin
self2._bt_Add.grid(row=1, column=2, padx=5, pady=5)#postioning / dimensions of button box
#labels for output box of combined binary number
label = tkinter.Label(self2._window, text="Added Binary: ", font= f)#label defined for number input box
label.grid(row=4, column=0, padx=10, pady=5)#positioning / dimensions of input box
#label for output box for if there was or was not an overflow
label = tkinter.Label(self2._window, text="OverFlow: ", font= f)#label defined for number input box
label.grid(row=5, column=0, padx=10, pady=5)#positioning / dimensions of input box
#output box for the added binary number
self2.stringvar_Combined = tkinter.StringVar()
txt_output = tkinter.Entry(self2._window, textvariable=self2.stringvar_Combined, width=TxtMaxLen, state="readonly", font= f)#entry box set to readonly to act as a display box
txt_output.grid(row=4, column=1, pady=5)
#output box for if there was or was not an overflow
self2._stringvar_OverFlow = tkinter.StringVar()
txt_output = tkinter.Entry(self2._window, textvariable=self2._stringvar_OverFlow, width=TxtMaxLen, state="readonly", font= f)#entry box set to readonly to act as a display box
txt_output.grid(row=5, column=1, pady=5)
separator = tkinter.ttk.Separator(self2._window,orient=tkinter.VERTICAL)
separator.grid(row=3, column=2, pady=15)
PrevPageButton = tkinter.Button(self2._window, text="Previous Page", font=f, command=MainWindow)
PrevPageButton.grid(row=5, column = 2, padx = 5,pady = 5)
def set_values(self, BinAdd):
self.stringvar_Combined.set(BinAdd)
def AdditionSelection(self2):
try:
BinV1 = self2.input1.get().strip().replace(" ", "")
BinV2 = self2.input2.get().strip().replace(" ", "")
BinAdd = BinaryAddition(BinV1, BinV2)
self2.set_values(BinAdd)
print(BinAdd)
print("hi there")
except Exception as ex:
print("NOPE it aint workin")
def __init__(self):
self._window = tkinter.Tk()
self._window.title("Converter")
self._window["bg"] = "#20A3FF" #background colour
self._window.geometry("820x240")#setting window size
#setting up button for binary additon page
NextPageButton = tkinter.Button(self._window, text="Next Page", font=MainWindow.FONT, command=self.SecondWindow)
NextPageButton.grid(row=4, column = 3, padx = 5,pady = 5)
self._window.destroy
def mainloop(self):
self._window.mainloop()
if __name__ == "__main__":
win = MainWindow()
win.mainloop()
主要问题:要创建第二个 window,您应该使用 Toplevel()
而不是 Tk()
。
当我使用 Toplevel
时,它会在 window 中显示文本。
如果你只想看到一个 window 那么你应该先 destroy()
旧的 window 然后你可以使用 Tk()
创建新的 window(新 mainloop()
)。
但这会产生其他问题。它会删除 window 中的所有值,当您返回一些 window 时,它将不再具有旧值。创建两个 Frames
并在不破坏它们的情况下将它们替换为 Tk()
可能更好 - 它们将保留旧值。
其余只是关于如何创建更具可读性的代码的建议:
- 不要把
SecondWindow
放在MainWindow
里面
- 不要使用
self2
- 对函数和变量使用
lower_case_names
PEP 8 -- Style Guide for Python Code
中的更多内容代码有一些变化。
显示数值(我还加了.zfill(8)
显示8位)
但是每次当您更改 window - next
或 previous
时,这都会创建新的 window - 因此您可能有两个主要的 windows,等等。它需要更多的改变。
import tkinter # PEP8: `import *` is not preferred
from tkinter import ttk
import math
import sys
def binary_addition(value1, value2):
if len(value1) != 8:
return "Binary number 1 is not 8 bits"
if len(value2) != 8:
return "Binary number 2 is not 8 bits"
res = bin(int(value1,2) + int(value2,2))
return res[2:].zfill(8) #removing the ob prefix
class MainWindow():
FONT = ("Consolas", 16)
TxtMaxLen = 32
def __init__(self):
self._window = tkinter.Tk()
self._window.title("Converter")
self._window["bg"] = "#20A3FF" #background colour
self._window.geometry("820x240")#setting window size
#setting up button for binary additon page
next_page_button = tkinter.Button(self._window, text="Next Page", font=MainWindow.FONT, command=SecondWindow)
next_page_button.grid(row=4, column = 3, padx = 5,pady = 5)
def mainloop(self):
self._window.mainloop()
class SecondWindow():
def __init__(self):
self._window = tkinter.Toplevel()
self._window.title("Converter")
self._window["bg"] = "#EFFFA3"
f = ("Times bold", 16)
TxtMaxLen = 32
self._window.geometry("820x240")
label = tkinter.Label(self._window, text="Binary Number 1: ", font= f)#label defined for number input box
label.grid(row=0, column=0, padx=10, pady=5)#positioning / dimensions of input box
label = tkinter.Label(self._window, text="Binary Number 2: ", font= f)#label defined for number input box
label.grid(row=1, column=0, padx=10, pady=5)#positioning / dimensions of input box
#input box for binary number 1
self.input1 = tkinter.Entry(self._window, width= TxtMaxLen, font= f) #defined input box
self.input1.grid(row=0, column=1, pady=5)#postioning / dimensions of input box
self.input1.focus()
#input box for binary number 2
self.input2 = tkinter.Entry(self._window, width=TxtMaxLen, font= f) #defined input box
self.input2.grid(row=1, column=1, pady=5)#postioning / dimensions of input box
self.input2.focus()
separator = tkinter.ttk.Separator(self._window,orient=tkinter.HORIZONTAL)
separator.grid(row=3, column=1, pady=15)
self._bt_add = tkinter.Button(self._window, text="Add", font= f, command = self.addition_selection)#button defined for bin
self._bt_add.grid(row=1, column=2, padx=5, pady=5)#postioning / dimensions of button box
#labels for output box of combined binary number
label = tkinter.Label(self._window, text="Added Binary: ", font= f)#label defined for number input box
label.grid(row=4, column=0, padx=10, pady=5)#positioning / dimensions of input box
#label for output box for if there was or was not an overflow
label = tkinter.Label(self._window, text="OverFlow: ", font= f)#label defined for number input box
label.grid(row=5, column=0, padx=10, pady=5)#positioning / dimensions of input box
#output box for the added binary number
self.stringvar_combined = tkinter.StringVar()
txt_output = tkinter.Entry(self._window, textvariable=self.stringvar_combined, width=TxtMaxLen, state="readonly", font= f)#entry box set to readonly to act as a display box
txt_output.grid(row=4, column=1, pady=5)
#output box for if there was or was not an overflow
self._stringvar_overflow = tkinter.StringVar()
txt_output = tkinter.Entry(self._window, textvariable=self._stringvar_overflow, width=TxtMaxLen, state="readonly", font= f)#entry box set to readonly to act as a display box
txt_output.grid(row=5, column=1, pady=5)
separator = tkinter.ttk.Separator(self._window,orient=tkinter.VERTICAL)
separator.grid(row=3, column=2, pady=15)
prev_page_button = tkinter.Button(self._window, text="Previous Page", font=f, command=MainWindow)
prev_page_button.grid(row=5, column = 2, padx = 5,pady = 5)
def set_values(self, value):
self.stringvar_combined.set(value)
def addition_selection(self):
try:
bin_1 = self.input1.get().strip().replace(" ", "")
bin_2 = self.input2.get().strip().replace(" ", "")
bin_add = binary_addition(bin_1, bin_2)
self.set_values(bin_add)
print("bin_add:", bin_add)
except Exception as ex:
print("NOPE it aint workin", ex)
if __name__ == "__main__":
win = MainWindow()
win.mainloop()