您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關如何在python中使用tkinter控件進行布局,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
代碼部分:
from tkinter import * import tkinter.messagebox as messagebox class Tkdemo(): def __init__(self): master = Tk() master.title("missWjz") master.geometry('800x800') #創建菜單欄 menubar = Menu(master) master.config(menu=menubar) #創建下拉菜單欄 filemenu = Menu(menubar,tearoff=0) menubar.add_cascade(label="文件",menu=filemenu) filemenu.add_command(label="新建",command=self.newfile) filemenu.add_command(label="打開",command=self.openfile) filemenu.add_command(label="保存",command=self.savefile) #增加分割線 filemenu.add_separator() filemenu.add_command(label="關閉填寫",command=master.quit) #Label(文字) title = Label(master,text="這是一份Tkinter的問卷調查",font=15,bg='white',fg='red') title.pack() #Frame 將問題1放在frame_1中 frame_1 = Frame(master) frame_1.pack(fill=X) #問題1 label_1 = Label(frame_1,text="1、我們第一次見面的地方?") label_1.grid(row=1,column=0) #Entry 問題一的輸入框 self.name = StringVar() entryname = Entry(frame_1,textvariable=self.name) #通過grid方法定位 entryname.grid(row=1,column=1) #Button 按鈕 getname = Button(frame_1,text="點擊確認",command=self.getname) getname.grid(row=1,column=3) #Frame 將問題二放在frame_2中 frame_2 = Frame(master) frame_2.pack(fill=X) #Label 問題二 label_2 = Label(frame_2,text="2、我記得你生日") label_2.grid(row=1,column=0) #Radiobutton 選擇按鈕 self.sex = StringVar() sex_male = Radiobutton(frame_2,text='記得',fg='blue',variable=self.sex,value='記得',command=self.getsex) sex_male.grid(row=1,column=2) sex_female = Radiobutton(frame_2,text='不可能',fg='red',variable=self.sex,value="不可能",command=self.getsex) sex_female.grid(row=1,column=4) #Frame 問題3 放在frame_3中 frame_3 = Frame(master) frame_3.pack(fill=X) #Label 問題3 label_3 = Label(frame_3,text="3、你對我的重要性:") label_3.grid(row=1,column=0) #Scale 滑動條 self.age = Scale(frame_3,from_=0,to=100,orient=HORIZONTAL,resolution=1) self.age.grid(row=1,column=1) #Button 按鈕 getage = Button(frame_3,text='點我確認',command=self.getage) getage.grid(row=1,column=2) #問題4 置于 Frame_4 中 frame_4 = Frame(master) frame_4.pack(fill=X) #問題四 label_4 = Label(frame_4,text="4、去除一段過去:") label_4.grid(row=1,column=0) #列表 self.listbox = Listbox(frame_4) self.listbox.grid(row=1,column=1) for item in ["第一次看電影","第一次拍照留影","第一次一起吃飯","第一次一起出去玩"]: self.listbox.insert(END,item) #s刪除按鈕 DELE = Button(frame_4,text="刪除",command=lambda listbox=self.listbox: listbox.delete(ANCHOR)) DELE.grid(row=1,column=1) #確認按鈕 review = Button(frame_4,text='點我確認',command = self.review) review.grid(row=1,column=2) #問題五 frame_5 = Frame(master) frame_5.pack(fill=X) label_5 = Label(frame_5,text='5、選擇一個圖案:') label_5.grid(row=1,column=0) self.canvas = Canvas(frame_5,width=200,height=200,bg='white') self.canvas.grid(row=1,column=1) self.pattern = StringVar() #圖案選擇按鈕 btn_first = Button(frame_5,text = "第一個",command =self.First_display) btn_second = Button(frame_5,text = "第二個",command =self.Second_dispaly) btn_third = Button(frame_5,text = "第三個",command =self.Third_display) btString = Button(frame_5, text="確定", command=self.displayString) btn_first.grid(row=2,column=2) btn_second.grid(row=2,column=3) btn_third.grid(row=2,column=4) btString.grid(row=2,column=5) #問題六 Frame_6 frame_6 = Frame(master) frame_6.pack(fill=X) label_6 = Label(frame_6,text="6、在一起的天數:") label_6.grid(row=1,column=0) #(Spinbox) self.number = Spinbox(frame_6,from_=0,to=200) self.number.grid(row=1,column=1) #確定按鈕 number = Button(frame_6,text="確定",command=self.getnumber) number.grid(row=1,column=2) #問題7 frame_7 frame_7 = Frame(master) frame_7.pack(fill=X) #問題 label_7 = Label(frame_7,text="7、印象最深的一天:") label_7.grid(row=1,column=0) #滾輪 scrollbar = Scrollbar(frame_7) scrollbar.grid(row=1,column=1) #列表 self.listbox2 = Listbox(frame_7,height=5,yscrollcommand=scrollbar.set) for i in range(99): self.listbox2.insert(END,str(i)) self.listbox2.grid(row=1,column=1) #確定按鈕 star = Button(frame_7,text="確定",command=self.getstar) star.grid(row=2,column=1) #問題8 frame_8 = Frame(master) frame_8.pack(fill=X) self.agree = StringVar() #Checkbutton 勾選按鈕 agree = Checkbutton(frame_8,text="我確定此問卷信息準確無誤",variable=self.agree,offvalue='不確定',command=self.getagree) agree.grid(row=1,column=1) #問題9 Frame_9 frame_9 = Frame(master) frame_9.pack() submit = Button(frame_9,text="提交",command=self.allsubmit) submit.grid(row=1,column=1) #問題十 frame_10 = Frame(master) frame_10.pack() #容器框 self.group = LabelFrame(frame_10,text="特別鳴謝",padx=5,pady=5) self.group.grid() w = Label(self.group,text="本問卷由衷感謝:https://github.com/James95107/Tkinter/blob/master") w.pack() master.mainloop() def getname(self): name = self.name.get() print(name) def getsex(self): sex = self.sex.get() print(sex) def getage(self): print(self.age.get()) def review(self): print(self.listbox.get(0, END)) def getstar(self): print(self.listbox2.get(ACTIVE)) def displayString(self): self.canvas.create_text(60, 40, text="您真棒!!!", font="Tine 10 bold underline", tags="string") print(self.pattern) def getnumber(self): print(self.getnumber()) def getagree(self): print(self.agree.get()) #打印心形 def First_display(self): self.canvas.create_arc((5, 5, 85, 85), width=2, outline="red", start=0, extent=180, style=ARC) self.canvas.create_arc((85, 5, 165, 85), width=2, outline="red", start=0, extent=180, style=ARC) self.canvas.create_arc((5, -45, 165, 125), width=2, outline="red", start=180, extent=180, style=ARC) self.pattern = 'First' #打印丘比特心形 def Second_dispaly(self): self.canvas.create_line((0, 55, 200, 55), width=2, fill='red', arrow=LAST, arrowshape=(20, 40, 10) ) self.canvas.create_arc((5, 5, 85, 85), width=2, outline="red", start=0, extent=180, style=ARC) self.canvas.create_arc((85, 5, 165, 85), width=2, outline="red", start=0, extent=180, style=ARC) self.canvas.create_arc((5, -45, 165, 125), width=2, outline="red", start=180, extent=180, style=ARC) self.pattern = 'Second' #打印愛心 def Third_display(self): self.canvas.create_text((90, 70), text="武峻竹", font=('微軟雅黑', 20, 'bold'), fill='red' ) self.canvas.create_arc((5, 5, 85, 85), width=2, outline="red", start=0, extent=180, style=ARC) self.canvas.create_arc((85, 5, 165, 85), width=2, outline="red", start=0, extent=180, style=ARC) self.canvas.create_arc((5, -45, 165, 125), width=2, outline="red", start=180, extent=180, style=ARC) self.pattern = 'Third' #屬性 #文件欄 def newfile(self): self.file = open(r"wujunzhu.txt",'w') self.file.close() messagebox.showinfo("創建文件","您已經成功創建個人資料文檔") #顯示對話框 def openfile(self): f = open(r"wujunzhu.txt",'r') try: f_read = f.read() print(f_read) finally: f.close() def savefile(self): messagebox.showwarning("保存文件","提交即保存!") def allsubmit(self): with open('D:cpwords.txt','w',encoding='utf-8') as f: f.write('我們第一見面的地方是:') f.write(self.name.get()) f.write('\n我記得你生日:') f.write(self.sex.get()) f.write('\n你對我的重要性:') f.write(str(self.age.get())) f.write('\n要去除一段過去是:') for i in self.listbox.get(0, END): f.write(i) f.write(" ,") f.write('\n在一起的天數是:') f.write(self.number.get()) f.write('\n印象最深的一天是:') f.write(self.listbox2.get(ACTIVE)) f.write('\n') f.write(self.agree.get()) f.write('本調查問卷的真實性') messagebox.showinfo('Success', '恭喜您已成功提交 ') # 顯示對話框 Tkdemo()
執行結果:
看完上述內容,你們對如何在python中使用tkinter控件進行布局有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。