Python解压缩文件

Python Unzip file

确实对 python 缺乏经验,但仍在学习中。我正在尝试通过单击按钮来解压缩文件。当我 运行 这个程序时,GUI 不显示按钮。

from zipfile import ZipFile
import Tkinter


top = Tkinter.Tk()
top.title("NAME of Program")


def Unzip():
with ZipFile ('Test.zip', 'r') as zipObj:
    zipObj.extractall()


UnButton = Tkinter.Button(Text="Unzip", command = Unzip)


top.mainloop()

您需要实施一些更改。
首先导入“tkinter”,而不是“Tkinter”。其次,您需要将“文本”更改为“文本”。最后,您需要使用“UnButton.pack()”

来打包按钮
from zipfile import ZipFile
import tkinter  # Changed


top = tkinter.Tk()  # Changed
top.title("NAME of Program")


def Unzip():
    with ZipFile ('Test.zip', 'r') as zipObj:  # Adding a Tab (4 spaces)
        zipObj.extractall()


UnButton = tkinter.Button(text="Unzip", command = Unzip)  # Changed
UnButton.pack()  # Added


top.mainloop()

好吧,我使用 canvas 方法重写了它,因为这将使您可以更好地控制应用程序的定位、添加图像、添加形状和移动形状。希望这对您有所帮助,编码愉快!!

from zipfile import ZipFile
from tkinter import *

root = Tk()
root.title("NAME of Program")
canvas = Canvas(width=500, height=500)
canvas.pack(fill="both", expand=True)


def unzip():
    with ZipFile('Test.zip', 'r') as zipObj:
        zipObj.extractall()


UnButton = Button(text="Unzip", command=unzip)
# First value is x coordinate, horizontal axis, second value is y coordinate vertical axis, window should equal the Variable of the
# tkinter widget
# you can create as many windows for canvas as you need for each element
canvas.create_window(235, 150, anchor="nw", window=UnButton)

mainloop()

您的代码已修复。 'UnButton.pack()'在代码中添加。

Follow this link to understand Widget.Pack() method

https://www.tutorialspoint.com/python/tk_pack.htm

from zipfile import ZipFile
import tkinter


top = tkinter.Tk()
top.title("NAME of Program")


def Unzip():
    with ZipFile ('Test.zip', 'r') as zipObj:
        zipObj.extractall()


UnButton = tkinter.Button(text="Unzip", command = Unzip)
UnButton.pack()


top.mainloop()