[Python] tkinter Code that is likely to be reused

import

import tkinter as tk

Main window

# Main window generation
tk1 = tk.Tk()
# Specify window size
tk1.geometry("900x600")
# Specify window title
tk1.title("TextConv 2020")
# Window size changeability setting
tk1.resizable(0,0)
# Window background color
tk1.configure(bg="white")

tk1.mainloop()

tk1.withdraw () # Do not suppress window display

menu bar

from tkinter import filedialog

def m_load():
 typ = [('text file','* .txt')]
    dir = 'C:\\'
    file = filedialog.askopenfilename(filetypes = typ, initialdir = dir) 
    f = open(file)
 fdata = f.read () # Returns all read data up to the end of the file
    textField.delete('1.0', 'end')
    textField.insert('1.0', fdata)
    f.close()

def m_save():
    file = filedialog.asksaveasfilename()
    txt = textField.get('1.0', 'end -1c')
    f=open(file, mode='w') 
    f.write(txt)
    f.close()

# Create menu bar
men = tk.Menu(tk1) 

# Set the menu bar on the screen
tk1.config(menu=men) 

# Create a parent menu (file) in the menu
menu_file = tk.Menu(tk1) 
men.add_cascade(label='File', menu=menu_file) 
men.add_cascade(label='File Load', command=m_load) 
men.add_cascade(label='File Save', command=m_save) 

Panel window

## orient: vertical or horizontal
## bg: Border color
 pack
## expand: Variable (True or False)
## fill: Movement when space is available (tk.BOTH spreads vertically and horizontally)
## side: From which direction to pack when arranging (side or top ...)

pw_main = tk.PanedWindow(tk1, orient='horizontal')
pw_main.pack(expand=True, fill = tk.BOTH, side="left")

pw_left = tk.PanedWindow(pw_main, bg="cyan", orient='vertical')
pw_main.add(pw_left)
pw_right = tk.PanedWindow(pw_main, bg="white", orient='vertical')
pw_main.add(pw_right)

flame

mainFrame = tk.Frame(pw_left, width=450, height=600, bg="white")
# If propagation is set to False, the frame size will be width, height.
# If True, stick to the widget inside
mainFrame.propagate(False)
mainFrame.pack()

Clipboard

def c_button1_click():
 # Read from clipboard
    MOji1 = tk1.clipboard_get()
    #txt1.insert(0,"test")
 The #insert () method specifies the position in the first argument and the character to insert in the second argument.
    textField.delete('1.0', 'end')
    textField.insert('1.0', MOji1)
 # textField.insert ('1.0',' Aiueo \ n Aiu Aiu \ nabcABCabcABC \ n123123')

def c_button2_click():
 # Read from clipboard
    textField.clipboard_append

# Button (pasted from clipboard)
 c_button1 = tk.Button (clipboardFrame, text ='Paste from clipboard', command = c_button1_click, bg = "yellow")
c_button1.place(x=10, y=5)

# Button (copy to clipboard)
 c_button2 = tk.Button (clipboardFrame, text = "copy to clipboard", command = c_button2_click, bg = "gold")
c_button2.place(x=150, y=5)

textarea

# scroll bar
scrollbar = tk.Scrollbar(textFrame)
scrollbar.pack(side=tk.RIGHT, fill="y")

# Text box
textField = tk.Text(textFrame, width=500, height=500, bd=5, relief="groove")
textField.propagate(False)
textField.pack(side=tk.LEFT, padx=(0, 0), pady=(0, 0))
textField["yscrollcommand"] = scrollbar.set
 textField.insert ('1.0',' Aiueo \ nAiu \ nabcABCabcABC \ n123123')

padx: Lateral gap on the outside = 10 Both sides = (10,10) Left, right pady: outer vertical gap Because it shows the correlation position with the front and left ones, not the absolute coordinates When using (,), it is easier to set (n, 0) and 0 after it. The default is Tk.CENTER. In addition, Tk.W (left side), Tk.E (right side), Tk.N (top side), Tk.S (bottom side), Tk.NW (upper left), Tk.SW (lower left), Tk.NE (upper right), Tk.SE (lower right)

Check button

 chk1 = tk.Checkbutton (SideFrame1, text ='line', bg = "lightgreen")
chk1.place(x=180, y=7)

button

 S_button1 = tk.Button (SideFrame2, text = "replacement", command = lambda: c_button_click (10), bg = "salmon")

tab

import tkinter.ttk as ttk

note = ttk.Notebook(tk1)
tab = tk.Frame(note,height=100,width=100)

note.add(tab, text="Tab")

note.pack()

Recommended Posts

[Python] tkinter Code that is likely to be reused
[Python] pandas Code that is likely to be reused
Rewrite Python2 code to Python3 (2to3)
I felt that I ported the Python code to C ++ 98.
Weird Python error message ——Is that code really executed?
Run the output code with tkinter, saying "A, pretending to be B" in python
How to update Python Tkinter to 8.6
Convert python 3.x code to python 2.x
How to test that Exception is raised in python unittest
Processing of python3 that seems to be usable in paiza
I want to be able to run Python in VS Code
python> Is it possible to make in-line comments?> It seems that it has to be on multiple lines
[Python3] Code that can be used when you want to resize images in folder units
That Python code has no classes ...
python3.9 has new features that no one is paying attention to ...
Install packages that need to be compiled with Python3 with pip [Windows]
If Python code written by someone else is hard to decipher (Python)
With PEP8 and PEP257, Python coding that is not embarrassing to show to people!
[Python] A program that calculates the number of socks to be paired
Address to the bug that node.surface cannot be obtained with python3 + mecab
Python code that makes DNA sequences complementary strands Which method is faster?
[Python] Use pandas to extract △△ that maximizes ○○
Python 3.9 dict merge (`|`) seems to be useful
[Python / Tkinter] How to pass arguments to command
10 Python errors that are common to beginners
Tkinter could not be imported in Python
[Python / Tkinter] Connect keyboard shortcuts to Menu
How to use is and == in Python
[Python3] Code that can be used when you want to cut out an image in a specific size
[Python3] Code that can be used when you want to change the extension of an image at once
Atcoder Beginner Contest A, B Input summary Python that tends to be a problem
A solution to the problem that the Python version in Conda cannot be changed
I want to create a priority queue that can be updated in Python (2.7)
I tried to summarize the operations that are likely to be used with numpy-stl
How to install a Python library that can be used by pharmaceutical companies
[Introduction to Udemy Python3 + Application] 37. Techniques for determining that there is no value
[What is an algorithm? Introduction to Search Algorithm] ~ Python ~
Python has come to be recognized so far.
Python script to convert latitude / longitude to mesh code
[Python / Tkinter] A class that creates a scrollable Frame
Python beginners tried to code some energy drinks
Python list comprehensions that are easy to forget
Personal notes to doc Python code in Sphinx
[Road to Intermediate] Python seems to be all objects
Python code that removes contiguous spaces into one
[Python] There seems to be something called __dict__
How to use tkinter with python in pyenv
Since sudo is that, try switching to doas
List of Python code to move and remember
I want to make C ++ code from Python code!
[Introduction to Udemy Python 3 + Application] 54. What is Docstrings?
Convert cubic mesh code to WKT in Python
How to use pip, a package management system that is indispensable for using Python
mong --I tried porting the code that randomly generates Docker container names to Python -
What to do if Python IntelliSense is not displayed in VS Code on Windows
[Python] Introduction to web scraping | Summary of methods that can be used with webdriver
A mechanism to call a Ruby method from Python that can be done in 200 lines