tkinter - 在包含单选按钮的python中关闭一个窗口并打开另一个窗口
问题描述
我对 python 比较陌生,所以可能有一个简单的答案。我尝试使用此示例在打开另一个窗口时关闭当前窗口将其合并,但它只是不起作用。我认为问题在于我没有正确使用类,但我似乎无法弄清楚。
提前致谢。
这是代码:
from tkinter import*
from tkinter import ttk
from tkinter import messagebox
import random
from tkinter.constants import HORIZONTAL
from turtle import left, width
root = Tk()
def replace_window(root):
"""Destroy current window, create new window"""
global current_window
#if current_window is not None:
# current_window.destroy()
current_window = Toplevel(root)
# if the user kills the window via the window manager,
# exit the application.
#current_window.wm_protocol("WM_DELETE_WINDOW", root.destroy)
#return current_window
def new_window():
window = replace_window(root)
label = Label(window, text="This is window" )
label.pack(fill="both", expand=True, padx=20, pady=20)
button.pack(padx=10, pady=10)
root.geometry('300x220')
root.resizable(False, False)
root.title('Water')
replace_window
def final_drink():
if selected_option.get() == 2:
messagebox.showinfo(title = "Congratulations!", message = "Water")
root.quit()
else:
messagebox.showinfo(title = "Wrong! Try again.", message ="Remember ")
new_window
def sel():
if selected_option.get() ==1:
messagebox.showinfo(title = "A Friendly Message", message = 'Hello, Tkinter!')
new_window
final_drink
else:
messagebox.showinfo(title = "Wrong! Try again.", message ="Remember")
selected_option = IntVar()
options = (('Removal', '1'),
('Dis', '2'),
('Clumping ', '3'),
('Separation', '4'))
# label
label = ttk.Label(text="What should you do now")
label.pack(fill='x', padx=5, pady=5)
# radio buttons
for option in options:
r = ttk.Radiobutton(
root,
text=option[0],
value=option[1],
variable=selected_option
)
r.pack(fill='x', padx=5, pady=5)
button1 = ttk.Button(
root,
text="Pick one!",
command=sel)
button1.pack(fill='x', padx=5, pady=5)
button = ttk.Button(
root,
text="Pick one!",
command=final_drink)
button.pack(fill='x', padx=5, pady=5)
label = Label(root)
label.pack()
root.mainloop()
解决方案
推荐阅读
- python - Python Tkinter 按钮删除
- scala - org.apache.spark.SparkException:无法执行用户定义的函数流式传输作业
- graphviz - Graphiz:方向节点为 L
- phpmyadmin - 我在 xampp/phpmyadmin 中导入了以下数据库。但这显示以下错误
- mysql - LOAD DATA INFILE - MySQL - 查找和替换(删除)一些关键字 - 在导入之前
- javascript - 如何在 JavaScript 文件中获取 API 并将其内容打印在网页上?
- swift - iOS 14 中的问题 UIDocumentPickerViewController
- bash - 用于修改文件夹和子文件夹中的文件但不修改文件夹名称的 Bash / Shell 脚本
- plot - PyVista `add_volume`:输出乱码 & 比 ParaView 慢
- c++ - 如何让程序仅通过输入多边形名称就知道边数?