首页 \ 问答 \ Python - tkinter:打开和关闭对话窗口(Python - tkinter: Opening and closing dialog windows)

Python - tkinter:打开和关闭对话窗口(Python - tkinter: Opening and closing dialog windows)

我对Python非常陌生,不得不写一个简单的GUI程序,为了简单起见,我选择在tkinter中做这个程序。

我想要的图形用户界面应该与在Windows上安装程序时经常遇到的对话框非常相似(您想在哪里安装,需要什么模块等)。 基本上,当它在python3.3中运行时,我想要一个窗口出现,其中一些选项占据了大部分窗口,底部的'next','back'和'cancel'按钮; 通过点击'下一步'按钮,当前窗口关闭,并打开一个新窗口,除了它有不同的选项(或者它是同一个窗口,但其内容已被销毁,我不确定会更好)。 我想要的粗略布局显示在此图像中 粗糙的布局

我查看了一些代码,它们做了类似的事情,但一直没能找到任何代码。 我已经看到了这个答案 ,但这不是我想要的。 我已经使用这个教程来了解我对tkinter的了解,但是我无法找到答案。

下面是我想要做的简化版本的极其糟糕的尝试:当我运行代码时,它会创建一个带有两个按钮的窗口。 “退出”按钮正常工作; 然而,当我点击“下一步”按钮时,它会关闭窗口并根据需要打开一个新窗口,但它也会打开另一个窗口。

from tkinter import *
from tkinter import ttk

def win1():
    mainframe = ttk.Frame(root, padding = '3 3 12 12')
    mainframe.grid(column = 0, row = 0, sticky = (N, W, E, S))
    mainframe.columnconfigure(0, weight = 1)
    mainframe.rowconfigure(0, weight = 1)

    ttk.Button(mainframe, text = 'Next', command = win2).grid(
        column = 1, row = 1, sticky = W)
    ttk.Button(mainframe, text = 'Quit', command = quit).grid(
        column = 1, row = 2, sticky = W)

    root.mainloop()

def quit():
    root.destroy()

def win2():
    quit()
    new = Toplevel()
    new.title('Window 2')
    new = ttk.Frame(root, padding = '3 3 12 12')
    new.grid(column = 0, row = 0, sticky = (N, W, E, S))
    new.columnconfigure(0, weight = 1)
    new.rowconfigure(0, weight = 1)

    ttk.Button(mainframe, text = 'Next', command = win2).grid(
        column = 1, row = 1, sticky = W)

root = Tk()  
win1()

这给出了以下错误信息(我不明白):

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.3/tkinter/__init__.py", line 1478, in __call__
    return self.func(*args)
  File "<stdin>", line 23, in win2
  File "/usr/lib/python3.3/tkinter/ttk.py", line 733, in __init__
    Widget.__init__(self, master, "ttk::frame", kw)
  File "/usr/lib/python3.3/tkinter/ttk.py", line 553, in __init__
    tkinter.Widget.__init__(self, master, widgetname, kw=kw)
  File "/usr/lib/python3.3/tkinter/__init__.py", line 2078, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: this isn't a Tk applicationNULL main window

除了它实际上并没有做我想做的事情之外,我觉得我正在以完全错误的方式去做(定义函数中的窗口等),并且当我想要遇到很多麻烦时使其更加复杂。 任何人都可以用更好的方式重写我的代码,并以一种方式帮助我构建更复杂的程序,提供资源来学习制作我想要的程序所需的内容,甚至提供建议? 谢谢。


I am very new to Python and have to write a simple GUI program, which I chose to do in tkinter for simplicity.

The GUI I want should be very similar to the dialog one often experiences when installing a program on Windows (where would you like to install, what modules would you like, etc). Basically when it is run in python3.3, I want a window to appear with some options taking up most of the window and 'next', 'back' and 'cancel' buttons at the bottom; by clicking the 'next' button, the current window is closed and a new one opens which looks the same, except that it has different options (or perhaps it is the same window but its contents have been destroyed, I'm not sure which would be better). The rough layout I want is shown in this image rough layout

I have looked around for example codes which do something similar to this but have not been able to find any. I have seen this answer, but it's not quite what I want. I have used this tutorial to learn what I know about tkinter but I can't find an answer in it.

Here is my extremely poor attempt at a simplified version of what I want to do: When I run the code it creates a window with two buttons. The 'Quit' button works fine; however, when I click the 'Next' button it closes the window and opens a new one as desired but it also opens another window.

from tkinter import *
from tkinter import ttk

def win1():
    mainframe = ttk.Frame(root, padding = '3 3 12 12')
    mainframe.grid(column = 0, row = 0, sticky = (N, W, E, S))
    mainframe.columnconfigure(0, weight = 1)
    mainframe.rowconfigure(0, weight = 1)

    ttk.Button(mainframe, text = 'Next', command = win2).grid(
        column = 1, row = 1, sticky = W)
    ttk.Button(mainframe, text = 'Quit', command = quit).grid(
        column = 1, row = 2, sticky = W)

    root.mainloop()

def quit():
    root.destroy()

def win2():
    quit()
    new = Toplevel()
    new.title('Window 2')
    new = ttk.Frame(root, padding = '3 3 12 12')
    new.grid(column = 0, row = 0, sticky = (N, W, E, S))
    new.columnconfigure(0, weight = 1)
    new.rowconfigure(0, weight = 1)

    ttk.Button(mainframe, text = 'Next', command = win2).grid(
        column = 1, row = 1, sticky = W)

root = Tk()  
win1()

This gives the following error message (which I don't understand):

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.3/tkinter/__init__.py", line 1478, in __call__
    return self.func(*args)
  File "<stdin>", line 23, in win2
  File "/usr/lib/python3.3/tkinter/ttk.py", line 733, in __init__
    Widget.__init__(self, master, "ttk::frame", kw)
  File "/usr/lib/python3.3/tkinter/ttk.py", line 553, in __init__
    tkinter.Widget.__init__(self, master, widgetname, kw=kw)
  File "/usr/lib/python3.3/tkinter/__init__.py", line 2078, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: this isn't a Tk applicationNULL main window

Apart from the fact that it doesn't actually do what I want, I feel like I am going about this the completely wrong way (defining the windows in a function, etc), and will run into a lot of trouble when I want to make it more complicated. Would anyone be able to rewrite my code in a better way and in a way that helps me build to more complicated programs, offer a resource for learning what I need to make the program I want or even offer advice? Thanks.


原文:https://stackoverflow.com/questions/24161936
更新时间:2022-02-08 06:02

最满意答案

永远不要将您的微调器适配器设置为null!

mySpinner.setAdapter(null);

我这样做是因为我在应用程序的功能中需要它。 所以我只是用mySpinner.setEnabled(false)来妥协。


Never set your spinner adapter to null!

mySpinner.setAdapter(null);

I did that because I needed it in my application's feature. So I just used mySpinner.setEnabled(false) to compromise.

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)