Tkinter(2.x 与3.X的区别)

时间:2022-12-06 22:15:55

1.包的引入

2.X下是

from Tkinter import *

而3.x是

from tkinter import *

否则,会报找不到tkinter的错误

Traceback (most recent call last):
File "read_data_from_gui.py", line 1, in <module>
from tkinter import *
ImportError: No module named tkinter

2.消息框

 # 2.X version
from Tkinter import *
import tkMessageBox def save_data():
try:
fileD = open("deliveries.txt", "a")
fileD.write("Depot:\n")
fileD.write("%s\n" % depot.get())
fileD.write("Description:\n")
fileD.write("%s\n" % description.get())
fileD.write("Address:\n")
fileD.write("%s\n" % address.get("1.0", END))
depot.set(None)
description.delete(0, END)
description.delete(0, END)
address.delete("1.0", END)
except Exception as ex:
tkMessageBox.showerror("Error!", "Can't write to the file\n %s" % ex) #3.x version
from tkinter import *
import tkinter.messagebox def save_data():
try:
fileD = open("deliveries.txt", "a")
fileD.write("Depot:\n")
fileD.write("%s\n" % depot.get())
fileD.write("Description:\n")
fileD.write("%s\n" % description.get())
fileD.write("Address:\n")
fileD.write("%s\n" % address.get("1.0", END))
depot.set(None)
description.delete(0, END)
description.delete(0, END)
address.delete("1.0", END)
except Exception as ex:
tkinter.messagebox.showerror("Error!", "Can't write to the file\n %s" % ex)