
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
import tkinter from tkinter import * from tkinter import messagebox import math from math import * from tkinter import scrolledtext #------------------------------------------------------------------------------------------ wd=Tk() wd.geometry("600x400") wd.title("ngoccs.edu.vn - MỘT SỐ BÀI TOÁN CƠ BẢN") wd.iconbitmap('ngoccs.ico') #------------------------------------------------------------------------------------------ def myOpt(): if (rv.get()==1): scrtxt.delete('1.0','end') txt_n2.delete(0,len(txt_n2.get())) txt_a.delete(0,len(txt_a.get())) txt_b.delete(0,len(txt_b.get())) txt_n1.focus_set() txt_n1.configure(state="normal") txt_n1.update() txt_n2.configure(state="disable") txt_n2.update() txt_a.configure(state="disable") txt_a.update() txt_b.configure(state="disable") txt_b.update() return 1 if (rv.get()==2): scrtxt.delete('1.0','end') txt_n1.delete(0,len(txt_n1.get())) txt_a.delete(0,len(txt_a.get())) txt_b.delete(0,len(txt_b.get())) txt_n2.focus_set() txt_n2.configure(state="normal") txt_n2.update() txt_n1.configure(state="disable") txt_n1.update() txt_a.configure(state="disable") txt_a.update() txt_b.configure(state="disable") txt_b.update() return 2 if (rv.get()==3): scrtxt.delete('1.0','end') txt_n1.delete(0,len(txt_n1.get())) txt_n2.delete(0,len(txt_n2.get())) txt_a.focus_set() txt_a.configure(state="normal") txt_a.update() txt_b.configure(state="normal") txt_b.update() txt_n1.configure(state="disable") txt_n1.update() txt_n2.configure(state="disable") txt_n2.update() #------------------------------------------------------------------------------------------ def is_SNT(x): if x==1: return False for i in range(2,int(sqrt(x))+1): if x%i==0: return False return True def list_SNT(N): l_snt="" d=0 for k in range(2,N+1): if is_SNT(k): d+=1 l_snt+=str(k)+" "; #if d%20==0: l_snt+='\n' return "Các SNT:" + l_snt def list_SCP(N): l_scp="" for k in range(1,int(sqrt(N))+1): l_scp+=str(k*k)+" " #if k%20==0: l_scp+='\n' return "Các SCP: " + l_scp def UCLN_ab(a,b): ao=a;bo=b while (a!=b): if a>b: a-=b else: b-=a; return "UCLN cua " +str(ao) +" va "+ str(bo) + " la:"+str(a) #------------------------------------------------------------------------------------------ def thoat(): wd.destroy() def xuli(): kq="" ok=myOpt() scrtxt.place(x=50,y=250,height=100,width=530) try: if ok==1: N=int(txt_n1.get()) pt=N/100 #lb_kq.config(text=list_SNT(N)) kq=list_SNT(N) elif ok==2: N=int(txt_n2.get()) #lb_kq.config(text=list_SCP(N)) kq=list_SCP(N) else: a=int(txt_a.get()) b=int(txt_b.get()) #lb_kq.config(text=UCLN_ab(a,b)) kq=UCLN_ab(a,b) scrtxt.insert(INSERT,kq) # Đưa kết quả lên scrolledText except: messagebox.showwarning("Cảnh báo","Xin lỗi! Dữ liệu vào không hợp lệ!") #------------------------------------------------------------------------------------------ lb=Label(wd,text="CHƯƠNG TRÌNH GIẢI MỘT SỐ BÀI TOÁN CƠ BẢN",font=('arial',14),fg='red') lb.place(x=70,y=20); #---------------------------------------------------------------------------------------------- scrtxt=scrolledtext.ScrolledText(wd,font=('cambria',12),fg='blue') #---------------------------------------------------------------------------------------------- rv=IntVar() rv.set(1) rdo1=Radiobutton(wd, text="Tìm các số nguyên tố [1-N]", variable=rv,value=1,command=myOpt) rdo1.place(x=50,y=100) rdo2=Radiobutton(wd, text="Liệt kê các số chính phương [1-N]", variable=rv,value=2,command=myOpt) rdo2.place(x=50,y=150) rdo3=Radiobutton(wd,text="Tìm UCLN của hai số a,b!",variable=rv,value=3,command=myOpt) rdo3.place(x=50,y=200) #------------------------------------------------------------------------------------------ txt_n1=Entry(wd,font=(12)) txt_n1.place(x=280,y=100,width=100) txt_n2=Entry(wd,font=(12),state="disable") txt_n2.place(x=280,y=150,width=100) txt_a=Entry(wd,font=(12),state="disable") txt_a.place(x=280,y=180,width=100) txt_b=Entry(wd,font=(12),state="disable") txt_b.place(x=280,y=210,width=100) #------------------------------------------------------------------------------------------ #lb_kq=Label(wd,text="",font=('arial',11),fg='blue',justify='left') #lb_kq.place(x=20,y=250); #------------------------------------------------------------------------------------------ def mou_in_o(even): cmd_ok['background']='yellow' def mou_out_o(even): cmd_ok['background']='SystemButtonFace' def mou_in_e(even): cmd_exit['background']='yellow' def mou_out_e(even): cmd_exit['background']='SystemButtonFace' cmd_ok=Button(wd,text="Thực hiện",font=("cambria",14),fg='blue',command=xuli) cmd_ok.place(x=400,y=120,width=100) cmd_ok.bind('<Enter>',mou_in_o) cmd_ok.bind('<Leave>',mou_out_o) cmd_exit=Button(wd,text="Thoát",font=("cambria",14),fg='red',command=thoat) cmd_exit.place(x=400,y=170,width=100) cmd_exit.bind('<Enter>',mou_in_e) cmd_exit.bind('<Leave>',mou_out_e) wd.mainloop() |