강의노트 기본 위젯 - button
조회수 134
• 댓글 0
• 수정 5개월 전
- 윈도우 프로그램
버튼
from tkinter import *
def fun_quit():
win.destroy()
win = Tk()
but = Button(win, text='파이썬 종료', fg = 'red', command=fun_quit)
but.pack()
win.mainloop()
from tkinter import *
from tkinter import messagebox
from PIL import ImageTk, Image
def fun():
messagebox.showinfo('이미지 테스트','ㅎㅎㅎ')
win = Tk()
photo = Image.open('D://전기전자//tiger.bmp') #1
im = ImageTk.PhotoImage(photo) #2
but = Button(win, image = im, command= fun)
but.pack()
win.mainloop()
버튼을 눌러서 버튼의 이미지를 바꾸는 방법
import tkinter as tk
count = 0
win = tk.Tk()
win.geometry("400x300") # Size of the window
bg = 'blue' # change the day colour
win.configure(background=bg) # default background of window
im1 = tk.PhotoImage(file = "D:\\전기전자\\clova_1.png")
im2 = tk.PhotoImage(file = "D:\\전기전자\\clova_2.png")
def f_change():
global count
if(count % 2 == 0 ):
b1.config(image=im1)
else:
b1.config(image=im2)
count = count + 1
b1=tk.Button(win,image=im1,relief='flat',bg=bg,command=lambda:f_change())
b1.grid(row=1,column=1,padx=20,pady=10)
win.mainloop() # Keep the window open
하나의 버튼에 여려개의 그림을 그리는 방법
import tkinter as tk
import random
count = 0
img = []
win = tk.Tk()
win.title('Card Select')
win.geometry("400x300")
bg = 'blue'
win.configure(background=bg)
for i in range(1,14):
s = f"D:\\전기전자\\clova_{i}.png"
im = tk.PhotoImage(file = s)
img.append(im)
def f_change():
im = random.choice(img)
b1.config(image=im)
b1=tk.Button(win,image=img[0],relief='flat',bg=bg,command=lambda:f_change())
b1.grid(row=1,column=1,padx=20,pady=10)
win.mainloop()
버튼의 command 함수에 변수를 추가해서 넘기려면 lambda함수를 사용해야 한다.
import tkinter as tk
import random
win = tk.Tk()
def f_com(var):
print(var)
b1=tk.Button(win,image=img[0],relief='flat',bg=bg,command=lambda:f_com(123))
b1.grid(row=1,column=1,padx=20,pady=10)
win.mainloop()
이전 글
다음 글
로그인 하면 댓글을 쓸 수 있습니다.