dearpygui 实现类似PyQt的 alert 弹窗

alert 弹窗

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import dearpygui.dearpygui as dpg

class Alert:
    ''' 弹出msg 窗口 '''
    def __init__(self, main_width, main_height):
        width = 600
        height = 130
        pos_x = (main_width - width)//2
        pos_y = (main_height - height)//2
        with dpg.window(label="warning", width=width, height=height, pos=(pos_x, pos_y), tag="alert_id", show=False):
            dpg.add_text(f"demo", color=[255,0,0],  tag='tag_alert_msg')
            with dpg.table(header_row=False):
                dpg.add_table_column()
                dpg.add_table_column()
                dpg.add_table_column()
                with dpg.table_row():
                    dpg.add_text(default_value='')
                    dpg.add_button(label="OK", width=-1, callback=self.yes_callback)
                    dpg.add_text(default_value='')
        
    def yes_callback(self):
        dpg.configure_item("alert_id", show=False)

    def alert_msg(self, title, msg):
        '''
        title: 窗口标题
        msg: 显示的信息
        '''
        dpg.configure_item('alert_id', label=title)
        dpg.configure_item('tag_alert_msg', default_value=msg)
        dpg.configure_item("alert_id", show=True)
        
        
class Window:
    main_window_width = 1600
    main_window_height = 650
    def __init__(self):
        dpg.create_context()
        with dpg.window(label="test_alert", width=self.main_window_width, height=self.main_window_height, tag='main_window'):
            self.alert = Alert(self.main_window_width, self.main_window_height)
            self.alert.alert_msg('title', 'message')
            dpg.set_primary_window('main_window', True)

        dpg.create_viewport(title='Custom Title', width=self.main_window_width, height=self.main_window_height)
        dpg.setup_dearpygui()
        dpg.show_viewport()
        dpg.start_dearpygui()
        dpg.destroy_context()

if __name__ == '__main__':        
    w = Window()

Yes Or No 弹窗

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import dearpygui.dearpygui as dpg

class YON:
    ''' 弹出yes or no 窗口 '''
    def __init__(self, main_width, main_height):
        self.ycb = None
        self.ncb = None
        
        width = 600
        height = 100
        pos_x = (main_width - width)//2
        pos_y = (main_height - height)//2
        with dpg.window(label="warning", width=width, height=height, pos=(pos_x, pos_y), tag="tag_yes_or_no_id", show=False):
            dpg.add_text(f"yes or no", color=[255,0,0],  tag='yes_or_no_test')
            dpg.add_separator()
            
            with dpg.table(header_row=False):
                dpg.add_table_column()
                dpg.add_table_column()
                dpg.add_table_column()
                dpg.add_table_column()
                with dpg.table_row():
                    dpg.add_text(default_value='')
                    dpg.add_button(label="YES", width=-1, callback=self.yes_callback)
                    dpg.add_button(label="NO", width=-1, callback=self.no_callback)
                    dpg.add_text(default_value='')
        
    def yes_callback(self):
        dpg.configure_item("tag_yes_or_no_id", show=False)
        if self.ycb:
            self.ycb()

    def no_callback(self):
        dpg.configure_item("tag_yes_or_no_id", show=False)
        if self.ncb:
            self.ncb()

    def yes_or_no(self, title, msg, ycb=None, ncb=None):
        '''
        ycb: 点击YES 时的回调函数
        ncb: 点击 NO 时的回调函数
        '''
        self.ycb = ycb
        self.ncb = ncb
        dpg.configure_item('tag_yes_or_no_id', label=title)
        dpg.set_value('yes_or_no_test', msg)    
        dpg.configure_item("tag_yes_or_no_id", show=True)
        
        
class Window:
    main_window_width = 1600
    main_window_height = 650
    def __init__(self):
        dpg.create_context()
        with dpg.window(label="test_yon", width=self.main_window_width, height=self.main_window_height, tag='main_window'):
            self.yon = YON(self.main_window_width, self.main_window_height)
            self.yon.yes_or_no('test title', 'test yes or no' ,self.yes_callback, self.no_callback)
            dpg.set_primary_window('main_window', True)

        dpg.create_viewport(title='Custom Title', width=self.main_window_width, height=self.main_window_height)
        dpg.setup_dearpygui()
        dpg.show_viewport()
        dpg.start_dearpygui()
        
        dpg.destroy_context()
    def yes_callback(self):
        print('yes callback')
        
    def no_callback(self):
        print('no callback')

if __name__ == '__main__':        
    w = Window()