MacOS zsh: segmentation fault python3 -u

173 Views Asked by At

My code is shown below:

import faulthandler
faulthandler.enable()

import numpy as np
import tkinter as tk
from tkinter import *
from tkinter.ttk import *
import os
from tkinter import messagebox
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt 
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from pandas import DataFrame


import BusinessLogic 
admin=BusinessLogic.BusinessLogic() 

current_user=1001 
current_time='2022/04/16' 
class MainWindow(Tk):
    def __init__(self,current_user,current_time):
        super().__init__()
        self.login_user=current_user
        self.login_time=current_time
        self.title('newdb')
        self.geometry('900x640+180+80')
        self.resizable(height=None,width=None)
        self['bg']='skyblue'
        self.setup_UI()
    
    def setup_UI(self):
       
        menubar=tk.Menu(self)
        menu_sub=tk.Menu(menubar,tearoff=0)
        menubar.add_cascade(label='book management',menu=menu_sub)
        menu_sub.add_command(label='book maintain',command=self.manage_bookGUI)
        menu_sub.add_command(label='book statics',command=self.sta_bookGUI)
        menu_sub.add_separator()
        menu_sub.add_command(label='quit',command=self.quit)
 
        help=tk.Menu(menubar,tearoff=0)
        help.add_command(label='About')
        menubar.add_cascade(label='help',menu=help)
        self.config(menu=menubar)
        
        #self.searchBtn=tk.Button(self,text='book maintain',command=self.manage_bookGUI)
        #self.searchBtn.place(x=0,y=0)
        #self.searchBtn=tk.Button(self,text='book statics',command=self.sta_bookGUI)
        #self.searchBtn.place(x=200,y=0)
        self.iconbitmap('~/Documents/programme/pythonProject/graduate/learning/copy/pic/member.ico')

        self.Style01=Style()
        self.Style01.configure('left.TPanedwindow',background='skyblue')
        self.Style01.configure('right.TPanedwindow',background='skyblue')
        self.Style01.configure('TButton',width=10)

self.Login_image=PhotoImage(file='~/Documents/programme/pythonProject/graduate/learning/copy/pic/Airport.png')
        self.Label_image=Label(self,image=self.Login_image)
        self.Label_image.pack()
        self.Label_login_user=Label(self,text='user'+str(self.login_user).title()+'\nloadtime:'+self.login_time)
        self.Label_login_user.place(x=700,y=40)

        self.Pane_left=PanedWindow(width=400,height=540,style='left.TPanedwindow')
        self.Pane_leftGUI()
        self.Pane_left1=PanedWindow(width=400,height=540,style='left.TPanedwindow')
        self.Pane_leftGUI_stabook()
        self.Pane_right=PanedWindow(width=400,height=540,style='right.TPanedwindow')
        self.Pane_rightGUI()

    def Pane_leftGUI(self): 
        self.LabelFrame_query=LabelFrame(self.Pane_left,text='book search',width=400,height=70)
        self.LabelFrame_query.place(x=10,y=0)

        self.Label_snosearch=Label(self.LabelFrame_query,text='bookcode:')
        self.Label_snosearch.place(x=5,y=13)
        self.Entry_snosearch=Entry(self.LabelFrame_query,width=8)
        self.Entry_snosearch.place(x=50,y=10)

        self.Button_query=Button(self.LabelFrame_query,text='search',command=self.search_book,width=4)
        self.Button_query.place(x=220,y=10)
        self.Button_all=Button(self.LabelFrame_query,text='search all',command=self.search_allbook,width=8)
        self.Button_all.place(x=290,y=10)

        self.Tree=Treeview(self.Pane_left,columns=('sno','names'),show='headings',height=20)
        self.Tree.column('sno',width=195,anchor='center')
        self.Tree.column('names',width=195,anchor='center')
        self.Tree.heading('sno',text='bookcode')
        self.Tree.heading('names',text='bookname')
        self.Tree.place(x=10,y=73)

        self.Tree.bind('<Double-1>',self.view_book)
        self.LabelFrame_query1=LabelFrame(self.Pane_left,text='book management',width=400,height=70)
        self.LabelFrame_query1.place(x=10,y=450)

        self.Label_sno=Label(self.LabelFrame_query1,text='bookcode:')
        self.Label_sno.place(x=5,y=13)
        self.Entry_sno=Entry(self.LabelFrame_query1,width=8)
        self.Entry_sno.place(x=50,y=10)
        self.Label_name=Label(self.LabelFrame_query1,text='bookname:')
        self.Label_name.place(x=125,y=13)
        self.Entry_name=Entry(self.LabelFrame_query1,width=8)
        self.Entry_name.place(x=170,y=10)
        self.Button_add=Button(self.LabelFrame_query1,text='add',command=self.add_book,style='TButton',width=4)
        self.Button_add.place(x=240,y=10)
        self.Button_update=Button(self.LabelFrame_query1,text='update',command=self.update_book,style='TButton',width=4)
        self.Button_update.place(x=290,y=10)
        self.Button_delete=Button(self.LabelFrame_query1,text='delete',command=self.delete_book,style='TButton',width=4)
        self.Button_delete.place(x=340,y=10)

    def Pane_leftGUI_stabook(self): 
        self.LabelFrame_query1=LabelFrame(self.Pane_left1,text='book statics',width=400,height=70)
        self.LabelFrame_query1.place(x=10,y=0)

        self.Tree1=Treeview(self.Pane_left1,columns=('sno','names'),show='headings',height=20)
    
        self.Tree1.column('sno',width=195,anchor='center')
        self.Tree1.column('names',width=195,anchor='center')

        self.Tree1.heading('sno',text='bookname')
        self.Tree1.heading('names',text='booknumber')

        self.Tree1.place(x=10,y=73)

    def Pane_rightGUI(self):
        self.figure_1=plt.figure(figsize=(4,5)) 
        self.ax=self.figure_1.add_subplot(1,1,1) 
        self.canvas_1=FigureCanvasTkAgg(self.figure_1,self.Pane_right)

    def load_treeview(self,current_list:list,tree:Treeview):
        if len(current_list)==0:
            messagebox.showinfo(title='newdb',message='the info does not exist')
        else:
            for index in range(len(current_list)):
                tree.insert('',index,values=(current_list[index][0],current_list[index][1]))
    def clear_treeview(self,tree:Treeview):
        for item in tree.get_children():
            tree.delete(item)

    def search_book(self):
        book=admin.book_getbyID(self.Entry_snosearch.get())
        self.clear_treeview(self.Tree)
        self.load_treeview(book,self.Tree)
    def search_allbook(self):
        book=admin.book_getall()
        self.clear_treeview(self.Tree)
        self.load_treeview(book,self.Tree)

    def add_book(self):
        result=admin.book_insert(self.Entry_sno.get(),self.Entry_name.get())
        if result=='OK':
            self.clear_treeview(self.Tree)
            book=admin.book_getall()
            self.load_treeview(book,self.Tree)
        else:
            messagebox.showinfo(title='newdb',message=result)
    def update_book(self):
        result=admin.book_update(self.Entry_sno.get(),self.Entry_name.get())
        if result=='OK':
            self.clear_treeview(self.Tree)
            book=admin.book_getall()
            self.load_treeview(book,self.Tree)
        else:
            messagebox.showinfo(title='newdb',message=result)
    def delete_book(self):
        result=admin.book_delete(self.Entry_sno.get())
        if result=='OK':
            self.clear_treeview(self.Tree)
            book=admin.book_getall()
            self.load_treeview(book,self.Tree)
        else:
            messagebox.showinfo(title='newdb',message=result)

    def view_book(self,event): 
        self.action_flag=1
        item=self.Tree.selection()[0] 
        Temp_publisher_list=self.Tree.item(item,'values') 
        self.Entry_sno.delete(0,END)
        self.Entry_name.delete(0,END)
        self.Entry_sno.insert(0,Temp_publisher_list[0])
        self.Entry_name.insert(1,Temp_publisher_list[1])

    def sta_book_sort(self):
        result=admin.book_sort_sta()
        if result:
            self.clear_treeview(self.Tree1)
            self.load_treeview(result,self.Tree1)
        self.Pane_right.place(x=410,y=95)
        self.ax.clear() 
        df=DataFrame([list(i) for i in result],columns=['type','number']) 
        df.index=df['type']
        df.plot(kind='bar',fontsize='small',rot=0,ax=self.ax)
        plt.title('total book',fontsize=12)
        plt.xlabel('type',fontsize=9)
        plt.ylabel('number',fontsize=9)
        self.canvas_1.draw()
        self.canvas_1.get_tk_widget().pack()

    def manage_bookGUI(self):
        self.Pane_left1.place_forget()
        self.Pane_left.place(x=4,y=95)
        self.Pane_right.place_forget()

    def sta_bookGUI(self):
        self.Pane_left.place_forget()
        self.Pane_left1.place(x=4,y=95)
        self.Pane_right.place(x=410,y=95)
        self.sta_book_sort()

    def quit(self):
        self.destroy()

The all I know is that after tk interface pops up, when I click the menubar I set, the interface will exit, and python will report the following error:

Fatal Python error: Segmentation fault

Current thread 0x000000010a8d1e00 (most recent call first):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/tkinter/__init__.py", line 1458 in mainloop
  File "/Users/apple/Documents/programme/pythonProject/graduate/learning/copy/tempCodeRunnerFile.py", line 66 in <module>

Extension modules: numpy.core._multiarray_umath, numpy.core._multiarray_tests, numpy.linalg._umath_linalg, numpy.fft._pocketfft_internal, numpy.random._common, numpy.random.bit_generator, numpy.random._bounded_integers, numpy.random._mt19937, numpy.random.mtrand, numpy.random._philox, numpy.random._pcg64, numpy.random._sfc64, numpy.random._generator, matplotlib._c_internal_utils, PIL._imaging, matplotlib._path, kiwisolver._cext, matplotlib._image, matplotlib.backends._tkagg, pandas._libs.tslibs.dtypes, pandas._libs.tslibs.base, pandas._libs.tslibs.np_datetime, pandas._libs.tslibs.nattype, pandas._libs.tslibs.timezones, pandas._libs.tslibs.ccalendar, pandas._libs.tslibs.tzconversion, pandas._libs.tslibs.strptime, pandas._libs.tslibs.fields, pandas._libs.tslibs.timedeltas, pandas._libs.tslibs.timestamps, pandas._libs.properties, pandas._libs.tslibs.offsets, pandas._libs.tslibs.parsing, pandas._libs.tslibs.conversion, pandas._libs.tslibs.period, pandas._libs.tslibs.vectorized, pandas._libs.ops_dispatch, pandas._libs.missing, pandas._libs.hashtable, pandas._libs.algos, pandas._libs.interval, pandas._libs.tslib, pandas._libs.lib, pandas._libs.hashing, pandas._libs.ops, pandas._libs.arrays, pandas._libs.index, pandas._libs.join, pandas._libs.sparse, pandas._libs.reduction, pandas._libs.indexing, pandas._libs.internals, pandas._libs.writers, pandas._libs.window.aggregations, pandas._libs.window.indexers, pandas._libs.reshape, pandas._libs.groupby, pandas._libs.testing, pandas._libs.parsers, pandas._libs.json, PIL._imagingtk (total: 61)
zsh: segmentation fault  python3 -u 

But there is no problem when I replace 'menubar' with 'button'. But I hope to be able to successfully use 'menubar' in tk, so how can I improve my code?

0

There are 0 best solutions below