RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1270738
Accepted
Булат
Булат
Asked:2022-04-16 03:09:33 +0000 UTC2022-04-16 03:09:33 +0000 UTC 2022-04-16 03:09:33 +0000 UTC

如何恢复隐藏窗口的功能?

  • 772

应用方法后withdraw,deiconify窗口冻结:它不响应命令,按钮,菜单等不起作用。

那么,真正的问题是该怎么做?

这是项目本身,它有一个“project.py”文件,您至少需要 1 个工作 COM 端口

来自 project.py 文件的代码

import serial.tools.list_ports
from tkinter import *
from tkinter import ttk
from Arduino import Arduino
from win32api import GetSystemMetrics, GetMonitorInfo, MonitorFromPoint
import time


monitor_info = GetMonitorInfo(MonitorFromPoint((0,0)))
monitor_area = monitor_info.get("Monitor")
work_area = monitor_info.get("Work")
task_bar = monitor_area[3] - work_area[3]


resx_win = str(int((GetSystemMetrics(0)/2)-(900/2)))
resy_win = str(int((GetSystemMetrics(1)/2)-(700/2) - task_bar))
resx_head = str(int((GetSystemMetrics(0)/2) - 800/2))
resy_head = str(int((GetSystemMetrics(1)/2) - (500/2)- task_bar))
warningx_head = str(int((GetSystemMetrics(0)/2)-300/2))
warningy_head = str(int((GetSystemMetrics(1) / 2) - (200 / 2) - task_bar))
resx_LED = str(int((GetSystemMetrics(0)/2)-950/2))
resy_LED = str(int((GetSystemMetrics(1)/2)-(600/2) - task_bar))


ports = serial.tools.list_ports.comports()
for port, desc, hwid in sorted(ports):
    port



head = Tk()

head.geometry('800x500+'+resx_head+'+'+resy_head)
head.title('Лаунчер')
head.resizable(width=False, height=False)
head.wm_attributes('-alpha', 0.95)
custom_bg = PhotoImage(file = 'custom_bg.png',master = head)
head.iconbitmap('window.ico')


def warning_port_baud_def():
    warning_port_baud = Toplevel()
    warning_port_baud.grab_set()
    warning_port_baud.geometry('300x200+' + warningx_head + '+' + warningy_head)
    warning_port_baud.title('Ошибка')
    warning_port_baud.iconbitmap('mistake.ico')
    warning_port_baud.resizable(width=False, height=False)
    warning_canvas = Canvas(warning_port_baud, width=300, height=200)
    warning_canvas.pack()
    warning_port_baud_frame = Frame(warning_port_baud)
    warning_port_baud_frame.place(relwidth=1, relheight=1)

    warning_port_baud_label = Label(warning_port_baud_frame,text='Введите \nнеобходимые\n значения '    #
                                                '\nдля порта \nи скорости', font= ("message-box",16))   #
    warning_port_baud_label.place(x=120, y=20, width=180, height=150)                                   # Окно,вылезающее при
    global warning_image                                                                                # некорректном вводе
    warning_image = PhotoImage(file='y_warning.png', master=warning_port_baud_frame)                    # порта или бодрейта
    warning_ico = Label(warning_port_baud_frame, image=warning_image)                                   # Ну и картинка для
    warning_ico.place(x=15, y=43)                                                                       # этого окна
    warning_port_baud.mainloop()

def warning_port_def():
    warning_port = Toplevel()
    warning_port.grab_set()
    warning_port.geometry('300x200+' + warningx_head + '+' + warningy_head)
    warning_port.title('Ошибка')
    warning_port.iconbitmap('mistake.ico')
    warning_port.resizable(width=False, height=False)
    warning_port_canvas = Canvas(warning_port,width=300, height=200)
    warning_port_canvas.pack()
    warning_port_frame = Frame(warning_port)
    warning_port_frame.place(relwidth=1, relheight=1)
    warning_port_label = Label(warning_port_frame, text='Некорректный\nввод порта',font=("message-box", 16))  #
    warning_port_label.place(x=120, y=20, width=180, height=150)               # Окно,вылезающее при
    global warning_image                                                            # некорректном вводе
    warning_image = PhotoImage(file='y_warning.png', master=warning_port_frame)     # порта
    warning_ico = Label(warning_port_frame, image=warning_image)                    # Ну и картинка
    warning_ico.place(x=15, y=43)
    warning_port.mainloop()

def warning_port_num_def():
    warning_port_num = Toplevel()
    warning_port_num.grab_set()
    warning_port_num.geometry('300x200+' + warningx_head + '+' + warningy_head)
    warning_port_num.title('Ошибка')
    warning_port_num.iconbitmap('mistake.ico')
    warning_port_num.resizable(width=False, height=False)
    warning_port_num_canvas = Canvas(warning_port_num,width=300, height=200)
    warning_port_num_canvas.pack()
    warning_port_num_frame = Frame(warning_port_num)
    warning_port_num_frame.place(relwidth=1, relheight=1)
    warning_port_num_label = Label(warning_port_num_frame, text='Некорректный\nввод порта:\nукажите '
                                'его\n номер\n[1,2,3 и т.д.]',font=("message-box", 16)) #
    warning_port_num_label.place(x=120, y=20, width=180, height=150)                   # Окно,вылезающее при
    global warning_image                                                                # некорректном вводе
    warning_image = PhotoImage(file='y_warning.png', master=warning_port_num_frame)     # номера порта
    warning_ico = Label(warning_port_num_frame, image=warning_image)                    # Ну и картинка
    warning_ico.place(x=15, y=43)
    warning_port_num.mainloop()

def warning_port_name_def():
    warning_port_name = Toplevel()
    warning_port_name.grab_set()
    warning_port_name.geometry('300x200+' + warningx_head + '+' + warningy_head)
    warning_port_name.title('Ошибка')
    warning_port_name.iconbitmap('mistake.ico')
    warning_port_name.resizable(width=False, height=False)
    warning_port_name_canvas = Canvas(warning_port_name,width=300, height=200)
    warning_port_name_canvas.pack()
    warning_port_name_frame = Frame(warning_port_name)
    warning_port_name_frame.place(relwidth=1, relheight=1)
    warning_port_name_label = Label(warning_port_name_frame, text='Некорректный\nввод порта:'
                                '\nданный порт\nне подключен',font=("message-box", 16))  #
    warning_port_name_label.place(x=120, y=20, width=180, height=150)                    # Окно,вылезающее при
    global warning_image                                                                 # некорректном вводе
    warning_image = PhotoImage(file='y_warning.png', master=warning_port_name_frame)     # порта
    warning_ico = Label(warning_port_name_frame, image=warning_image)                    # Ну и картинка
    warning_ico.place(x=15, y=43)
    warning_port_name.mainloop()

def warning_baud_def():
    warning_baud = Toplevel()
    warning_baud.grab_set()
    warning_baud.geometry('300x200+' + warningx_head + '+' + warningy_head)
    warning_baud.title('Ошибка')
    warning_baud.iconbitmap('mistake.ico')
    warning_baud.resizable(width=False, height=False)
    warning_baud_canvas = Canvas(warning_baud,width=300, height=200)
    warning_baud_canvas.pack()
    warning_baud_frame = Frame(warning_baud)
    warning_baud_frame.place(relwidth=1, relheight=1)
    warning_baud_label = Label(warning_baud_frame, text='Некорректный\nввод бодрейта:\nиспользуйте\nцифры',font=("message-box", 16))  #
    warning_baud_label.place(x=120, y=20, width=180, height=150)               # Окно,вылезающее при
    global warning_image                                                            # некорректном вводе
    warning_image = PhotoImage(file='y_warning.png', master=warning_baud_frame)     # порта
    warning_ico = Label(warning_baud_frame, image=warning_image)                    # Ну и картинка
    warning_ico.place(x=15, y=43)
    warning_baud.mainloop()

def warning_baud_min_def():
    warning_baud_min = Toplevel()
    warning_baud_min.grab_set()
    warning_baud_min.geometry('300x200+' + warningx_head + '+' + warningy_head)
    warning_baud_min.title('Ошибка')
    warning_baud_min.iconbitmap('mistake.ico')
    warning_baud_min.resizable(width=False, height=False)
    warning_baud_min_canvas = Canvas(warning_baud_min,width=300, height=200)
    warning_baud_min_canvas.pack()
    warning_baud_min_frame = Frame(warning_baud_min)
    warning_baud_min_frame.place(relwidth=1, relheight=1)
    warning_baud_min_label = Label(warning_baud_min_frame, text='Некорректный\nввод бодрейта\nМинимум: 300',font=("message-box", 16))  #
    warning_baud_min_label.place(x=120, y=20, width=180, height=150)               # Окно,вылезающее при
    global warning_image                                                            # некорректном вводе
    warning_image = PhotoImage(file='y_warning.png', master=warning_baud_min_frame)     # порта
    warning_ico = Label(warning_baud_min_frame, image=warning_image)                    # Ну и картинка
    warning_ico.place(x=15, y=43)
    warning_baud_min.mainloop()

def port_OlO_def():
    port_OlO = Toplevel()
    port_OlO.geometry('300x200+' + warningx_head + '+' + warningy_head)
    port_OlO.title('Ошибка')
    port_OlO.resizable(width=False, height=False)
    port_OlO.iconbitmap('mistake.ico')
    port_OlO_frame = Frame(port_OlO)
    port_OlO_frame.place(relwidth=1, relheight=1)
    port_OlO_label = Label(port_OlO_frame, text='Издеваетесь?',font=("message-box", 16))
    port_OlO_label.place(x=120, y=20, width=180, height=150)
    global warning_image
    warning_image = PhotoImage(file='y_warning.png', master=port_OlO_frame)
    warning_ico = Label(port_OlO_frame, image=warning_image)
    warning_ico.place(x=15, y=43)
    port_OlO.mainloop()

###################################


def Win():
    head.withdraw()
    win = Toplevel()
    win.geometry('900x700+' + resx_win + '+' + resy_win)
    win.title('Набор')
    win["bg"] = "#a17a86"
    win.iconbitmap('window.ico')
    win.resizable(width=False, height=False)
    win.protocol("WM_DELETE_WINDOW", lambda: head.destroy())    # Закрывает окно head,при закрытии этого

    frame_lobby = Frame(win)
    frame_lobby.place(relwidth = 1,relheight = 1)
    frame_lobby.configure(background = "#FFAEC2")

    LED_bg = PhotoImage(file = 'LED_bg.png')
    LCD_bg = PhotoImage(file = 'LCD_bg.png')
    buzzer_bg = PhotoImage(file = 'buzzer_bg.png')
    ultrasound_bg = PhotoImage(file = 'ultrasound_bg.png')
    servo_bg = PhotoImage(file = 'servo_bg.png')
    DS18B20_bg = PhotoImage(file = 'DS18B20_bg.png')

    help_button_image = PhotoImage(file = 'help_button_image.png')
    help_button_image_small = help_button_image.subsample(85, 85)


    def LED_window_def():
        win.withdraw()
        LED_window = Toplevel()
        LED_window.grab_set()
        LED_window.geometry('950x600+{}+{}'.format(resx_LED,resy_LED))
        LED_window.title("Светодиод")
        LED_window.resizable(width=False, height=False)
        LED_window.protocol("WM_DELETE_WINDOW", lambda: head.destroy())
        LED_frame = Frame(LED_window)
        LED_frame.place(relwidth = 1,relheight = 1)
        LED_frame.configure(background = "")

        LED_button_on_off = Label(LED_frame,text = 'Тут будет гифка ON/OFF')
        LED_button_on_off.place(x = 30,y = 120,width = 200,height = 130)

        LED_button_on_off_str = Label(LED_frame, text='Тут будет гифка ON/OFF--STR')
        LED_button_on_off_str.place(x = 260,y = 120,width = 200,height = 130)

        LED_button_bright = Label(LED_frame,text = 'Яркость (ШИМ со шкалой)')
        LED_button_bright.place(x = 490,y = 120,width = 200,height = 130)

        LED_button_bright_POT = Label(LED_frame,text = 'Яркость(потенциометр)')
        LED_button_bright_POT.place(x = 720,y = 120,width = 200,height = 130)

        tut_budet_bg = Label(LED_frame,text = 'Тут будет <BG>')
        tut_budet_bg.place(x = 325,y = 0 ,width = 300,height = 80)

        help_button = ttk.Button(LED_frame,text = 'Условие',image = help_button_image_small,compound = LEFT,)
        help_button_style = ttk.Style(head)
        help_button_style.configure('TButton', font=('lucida console', '15'),background = '#fafad2')
        help_button.place(x = 760,y = 15,width = 140,height = 45)


        def undo():
            win.deiconify()
            LED_window.withdraw()

        undo_LED_button = ttk.Button(LED_frame,text = 'Назад',command = undo)
        undo_LED_button.place(width = 100,height = 45)


    LED_button = Button(frame_lobby,image = LED_bg,command = LED_window_def)
    LED_button.place(x = 50,y = 20)
    ultrasound_button = Button(frame_lobby, image=ultrasound_bg)
    ultrasound_button.place(x = 50,y = 245)
    LCD_button = Button(frame_lobby,image = LCD_bg)
    LCD_button.place(x = 50,y = 470)
    buzzer_button = Button(frame_lobby,image = buzzer_bg)
    buzzer_button.place(x = 475,y= 20)
    servo_button = Button(frame_lobby,image = servo_bg)
    servo_button.place(x = 475,y = 245)
    DS18B20_button = Button(frame_lobby,image = DS18B20_bg)
    DS18B20_button.place(x = 475,y = 470)

    win.mainloop()

def WinSon():

    port_ch = (var_p.get())         #
    if port_ch == PortList[0]:      # Если в меню не выбран порт,
        port_ch = entry_p.get()     # читает поле ввода
        print(port_ch)              #

    baudrate = (var_bd.get())       #
    if baudrate == BaudList[0]:     # То же самое,
        baudrate = entry_bd.get()   # только уже с бодрейтом
        print(baudrate)             #
    index = port_ch.find("COM")


    if len(port_ch) == 0 or len(baudrate) == 0: #
        print(len(port_ch))                     # Проверка на длину введённых данных
        print(len(baudrate))                    # <Потом добавлю поиск подстроки 'COM'>
        warning_port_baud_def()                 #
    elif index == -1:
        warning_port_def()
    elif baudrate.isdigit() == False:
        warning_baud_def()
    elif int(baudrate) < 300:
        warning_baud_min_def()
    elif port_ch == 'COM':
        warning_port_num_def()
    elif port_ch != port:
        warning_port_name_def()
    else:
        Win()


frame = Frame(head)
frame.place(relwidth = 1,relheight = 1)

bg = PhotoImage(file = 'bg_800x500.png')    #
wallpaper = Label(frame,image = bg)         # Пикча для BG
wallpaper.pack()                            #


t_p = ttk.Label(frame, text='ПОРТ/выбери или напиши', background='#A52A2A',
                font = ('lucida console','14'),anchor = CENTER)
t_p.place(x=20, y = 220, width = 370, height = 60)
PortList = ['Выберите порт', port]
var_p = StringVar(frame)
var_p.set(PortList[0])

opt_p = ttk.OptionMenu(frame, var_p, *PortList)
opt_p.place(x=20, y=280, width=185, height=30)

entry_p = Entry(frame,font = 15)
entry_p.place(x = 205,y = 280,width = 185,height = 30)


t_bd = ttk.Label(frame,text = 'БОДРЕЙТ указанный в \nскетче ардуино|выбери или напиши',
    background = '#A52A2A',font = ('lucida console','14'),anchor = CENTER)
t_bd.place(x = 410,y = 220,width = 370,height = 60)
BaudList = ['Выберите скорость',300,1200,2400,4800,9600,19200,38400,57600,74880,115200]
var_bd = StringVar(frame)
var_bd.set(BaudList[0])
opt_bd = ttk.OptionMenu(frame, var_bd, *BaudList)
opt_bd.place(x = 410,y = 280,width = 185,height = 30)

entry_bd = Entry(frame, font=15)
entry_bd.place(x=595, y=280, width=185, height=30)


further = PhotoImage(file = 'далее.png')
small_further = further.subsample(15, 15)

further_Button_style = ttk.Style(head)
further_Button_style.configure('TButton',font = ('lucida console','15'))

further_Button = ttk.Button(frame,text = 'Далее',image = small_further,
    compound = LEFT,style = 'TButton',command = WinSon)          # WinSon
further_Button.place(x = 350,y = 370,width = 130,height = 50)




head.mainloop()

python
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Best Answer
    insolor
    2022-04-16T22:44:38Z2022-04-16T22:44:38Z

    问题是这样的:

    LED_window = Toplevel()
    LED_window.grab_set()  # <--
    
    ...
    
    def undo():
        win.deiconify()
        LED_window.withdraw()
    

    该方法.grab_set()使子窗口模态化,因此当它存在时,主窗口被阻塞。如果子窗口只是简单地隐藏,那么主窗口将保持阻塞状态。

    您需要.grab_set()使用以下方法取消操作.grab_release():

    def undo():
        win.deiconify()
        LED_window.grab_release()
        LED_window.withdraw()
    

    或者不是隐藏,而是销毁子窗口(特别是当你重新进入函数时,LED_window_def旧的子窗口没有返回,而是创建了一个新的,所以隐藏没有意义):

    def undo():
        win.deiconify()
        LED_window.destroy()
    

    第三种选择是不这样做.grab_set(),因为。无论如何,您之前的窗口都是隐藏的,因此阻止它们是没有意义的。您还可以结合最后两个选项:退出时不阻塞和销毁。

    • 3
  2. Zipi Code
    2022-04-16T13:09:17Z2022-04-16T13:09:17Z

    withdraw() 从屏幕上删除窗口(不破坏它)。要重绘窗口,请使用 deiconify。当窗口被移除时,状态方法返回“withdrawn”。

    from tkinter import *
    r = Tk()
    r.withdraw()
    r.deiconify()
    
    r.mainloop()
    
    • 1

相关问题

  • 是否可以以某种方式自定义 QTabWidget?

  • telebot.anihelper.ApiException 错误

  • Python。检查一个数字是否是 3 的幂。输出 无

  • 解析多个响应

  • 交换两个数组的元素,以便它们的新内容也反转

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    表格填充不起作用

    • 2 个回答
  • Marko Smith

    提示 50/50,有两个,其中一个是正确的

    • 1 个回答
  • Marko Smith

    在 PyQt5 中停止进程

    • 1 个回答
  • Marko Smith

    我的脚本不工作

    • 1 个回答
  • Marko Smith

    在文本文件中写入和读取列表

    • 2 个回答
  • Marko Smith

    如何像屏幕截图中那样并排排列这些块?

    • 1 个回答
  • Marko Smith

    确定文本文件中每一行的字符数

    • 2 个回答
  • Marko Smith

    将接口对象传递给 JAVA 构造函数

    • 1 个回答
  • Marko Smith

    正确更新数据库中的数据

    • 1 个回答
  • Marko Smith

    Python解析不是css

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5