Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在关闭当前窗口的同时打开新窗口?_Python_Tkinter - Fatal编程技术网

Python 如何在关闭当前窗口的同时打开新窗口?

Python 如何在关闭当前窗口的同时打开新窗口?,python,tkinter,Python,Tkinter,此代码打开一个菜单,该菜单链接到另一个菜单。第一个按钮不能同时关闭和打开一个新按钮,我如何解决这个问题 将tkinter作为tk导入 将tkinter.messagebox导入为box 类别enterle(tk.tk): 定义初始化(自): super()。\uuuu init\uuuuu() self.title('输入RLE') self.line\u count\u str=tk.StringVar() self.compressed_data_str=tk.StringVar(self)

此代码打开一个菜单,该菜单链接到另一个菜单。第一个按钮不能同时关闭和打开一个新按钮,我如何解决这个问题

将tkinter作为tk导入
将tkinter.messagebox导入为box
类别enterle(tk.tk):
定义初始化(自):
super()。\uuuu init\uuuuu()
self.title('输入RLE')
self.line\u count\u str=tk.StringVar()
self.compressed_data_str=tk.StringVar(self)
框架=传统框架(自身)
tk.Label(self,text='Line Count:').pack(padx=15,pady=5)
tk.Entry(self,bd=5,textvariable=self.line\u count\u str).pack(padx=15,pady=5)
按钮(self,text=“Next”,宽度=5,命令=self.line\u count\u func).pack(side='right',padx=5)
按钮(self,text='Exit',width=5,command=self.destroy).pack(side='right',padx=5)
框架组件(padx=100,pady=19)
定义行计数函数(自身):
如果self.line\u count\u str.get().isdigit():
如果int(self.line\u count\u str.get())小于3:
box.showinfo(title=“Error”,message=“输入超过3的数字”)
elif int(self.line\u count\u str.get())>100000000:
box.showinfo(title=“Error”,message=“输入100000000以下的数字”)
其他:
self.enter_rle_2()
def输入_rle_2(自身):
top=tk.Toplevel(自身)
top.title('输入RLE')
框架=传统框架(顶部)
tk.Label(top,text='Compressed Data:').pack(padx=15,pady=5)
tk.Entry(top,bd=5,textvariable=self.compressed_data_str).pack(padx=15,pady=5)
tk.按钮(顶部,text=“Next”).pack(侧部='右侧',padx=5)
机架组件(padx=19)
enterle1().mainloop()

我见过一些人使用一种新的“def”方法来处理这类事情,但我不确定如何使这些方法适应我的代码。

这里有几个问题

第一个主要问题是两次使用
Tk()。在这个特定的实例中,当清理代码时,它可以正常工作,但通常您不希望使用多个
Tk()
实例

第二个主要问题是您如何尝试在单击的
linecount\u按钮
功能中销毁
enter\rle
。您无法执行此操作,因为该函数不知道来自单独函数的任何有关
enter\rle
。您需要在按钮命令中传递它

第三个主要问题是
int(linecount\u STR.get())
。如果get抓取了除数字或空刺以外的任何东西,则会出错。所以你的
else
子句永远不会发生,因为它会在你的
if/else
语句之前出错。让我们用
isdigit()
解决这个问题

下一步,即使它不会损害任何东西,但您希望在函数顶部定义全局函数

这里有一些PEP8问题不会影响代码,但是如果您清理它们,它会更容易阅读

实际上,这可能应该构建在一个类中,这样我们就可以使用类属性和方法来管理一切

这是你的代码,如果你有任何问题,请告诉我

from tkinter import *
import tkinter.messagebox as box


def enter_rle_1():
    enter_rle = Tk()
    linecount_STR = StringVar()
    enter_rle.title('Enter RLE')
    frame = Frame(enter_rle)
    label_linecount = Label(enter_rle, text='Linecount:')
    label_linecount.pack(padx=15, pady=5)
    linecount = Entry(enter_rle, bd=5, textvariable=linecount_STR)
    linecount.pack(padx=15, pady=5)
    ok_button = Button(enter_rle, text="Next", width=5,
                       command=lambda lc=linecount_STR: linecount_button_clicked(enter_rle, lc))
    ok_button.pack(side=RIGHT, padx=5)
    stop = Button(enter_rle, text='Exit', width=5, command=enter_rle.destroy)
    stop.pack(side=RIGHT, padx=5)
    frame.pack(padx=100, pady=19)
    enter_rle.mainloop()


def linecount_button_clicked(enter_rle, linecount_STR):
    linecount = linecount_STR.get()
    if linecount.isdigit():  # does nothing if value is not a digit.
        if int(linecount) < 3:
            box.showinfo(title="Error", message="Enter a number over 3")
        elif int(linecount) > 1000000000:
            box.showinfo(title="Error", message="Enter a number under 1,000,000,000")
        else:
            enter_rle_2(enter_rle)


def enter_rle_2(root):
    enter_rle = Toplevel(root)
    compressed_data_STR = StringVar(root)
    enter_rle.title('Enter RLE')
    frame = Frame(enter_rle)
    label_compressed_data = Label(enter_rle, text='Compressed Data:')
    label_compressed_data.pack(padx=15, pady=5)
    compressed_data = Entry(enter_rle, bd=5, textvariable=compressed_data_STR)
    compressed_data.pack(padx=15, pady=5)
    ok_button = Button(enter_rle, text="Next")
    ok_button.pack(side=RIGHT, padx=5)
    frame.pack(padx=100, pady=19)


enter_rle_1()
从tkinter导入*
将tkinter.messagebox导入为box
def输入_rle_1():
输入_rle=Tk()
linecount_STR=StringVar()
输入标题('enter rle')
帧=帧(输入_rle)
label\u linecount=label(输入\u rle,text='linecount:')
label_linecount.pack(padx=15,pady=5)
linecount=Entry(输入\u rle,bd=5,textvariable=linecount\u STR)
linecount.pack(padx=15,pady=5)
确定按钮=按钮(输入按钮,text=“Next”,宽度=5,
command=lambda lc=linecount\u STR:linecount\u按钮\u单击(输入\u rle,lc))
确定按钮。组件(侧=右侧,padx=5)
停止=按钮(输入\ rle,text='Exit',宽度=5,命令=输入\ rle.destroy)
停止包装(侧面=右侧,padx=5)
框架组件(padx=100,pady=19)
输入_rle.mainloop()
def linecount_按钮_单击(输入rle,linecount_STR):
linecount=linecount\u STR.get()
if linecount.isdigit():#如果值不是数字,则不执行任何操作。
如果int(行数)<3:
box.showinfo(title=“Error”,message=“输入超过3的数字”)
elif int(行数)>100000000:
box.showinfo(title=“Error”,message=“输入100000000以下的数字”)
其他:
输入\u rle\u 2(输入\u rle)
def输入_rle_2(根):
输入\u rle=Toplevel(根)
压缩的\u数据\u STR=StringVar(根)
输入标题('enter rle')
帧=帧(输入_rle)
label\u compressed\u data=label(输入\u rle,text='compressed data:')
标签压缩数据包(padx=15,pady=5)
压缩数据=输入(输入rle,bd=5,textvariable=compressed\u data\u STR)
压缩数据包(padx=15,pady=5)
确定按钮=按钮(输入“下一步”)
确定按钮。组件(侧=右侧,padx=5)
框架组件(padx=100,pady=19)
输入_rle_1()
下面是使用类属性和方法的代码的OOP版本。在Tkinter中管理这种交互要容易得多,而且永远不需要全局的

import tkinter as tk
import tkinter.messagebox as box


class EnterRle1(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title('Enter RLE')
        self.line_count_str = tk.StringVar()
        self.compressed_data_str = tk.StringVar(self)

        frame = tk.Frame(self)
        tk.Label(self, text='Line Count:').pack(padx=15, pady=5)
        tk.Entry(self, bd=5, textvariable=self.line_count_str).pack(padx=15, pady=5)
        tk.Button(self, text="Next", width=5, command=self.line_count_func).pack(side='right', padx=5)
        tk.Button(self, text='Exit', width=5, command=self.destroy).pack(side='right', padx=5)
        frame.pack(padx=100, pady=19)

    def line_count_func(self):
        if self.line_count_str.get().isdigit():
            if int(self.line_count_str.get()) < 3:
                box.showinfo(title="Error", message="Enter a number over 3")
            elif int(self.line_count_str.get()) > 1000000000:
                box.showinfo(title="Error", message="Enter a number under 1,000,000,000")
            else:
                self.enter_rle_2()

    def enter_rle_2(self):
        top = tk.Toplevel(self)
        top.title('Enter RLE')
        frame = tk.Frame(top)
        tk.Label(top, text='Compressed Data:').pack(padx=15, pady=5)
        tk.Entry(top, bd=5, textvariable=self.compressed_data_str).pack(padx=15, pady=5)
        tk.Button(top, text="Next").pack(side='right', padx=5)
        frame.pack(padx=100, pady=19)


EnterRle1().mainloop()
将tkinter作为tk导入
将tkinter.messagebox导入为box
第1类(tk.tk):
定义初始化(自):
super()。\uuuu init\uuuuu()
self.title('输入RLE')
self.line\u count\u str=tk.StringVar()
self.compressed_data_str=tk.StringVar(self)
框架=传统框架(自身)
tk.Label(self,text='Line Count:').pack(padx=15,pady=5)
tk.Entry(self,bd=5,textvariable=self.line\u count\u str).pack(padx=15,pady=5)
按钮(self,text=“Next”,宽度=5,命令=self.line\u count\u func).pack(side='right',padx=5)
按钮(self,text='Exit',width=5,command=self.destroy).pack(side='right',padx=5)
框架组件(padx=100,pady=19)
定义行计数函数(自身):
如果self.line\u count\u str.get().isdigit():
如果int(self.line\u count\u str.get())小于3:
box.showinfo(title=“Error”,message=“输入超过3的数字”)
elif int(自行计数)